Выделение файлов приложения в рабочую директорию

This commit is contained in:
Book Pauk
2022-12-11 18:30:33 +07:00
parent 55ee303fc5
commit 5d5ad40f4e
8 changed files with 160 additions and 119 deletions

View File

@@ -2,18 +2,14 @@ const path = require('path');
const pckg = require('../../package.json');
const execDir = path.resolve(__dirname, '..');
const dataDir = `${execDir}/data`;
module.exports = {
branch: 'unknown',
version: pckg.version,
name: pckg.name,
dataDir: dataDir,
tempDir: `${dataDir}/tmp`,
logDir: `${dataDir}/log`,
publicDir: `${execDir}/public`,
uploadDir: `${execDir}/public/upload`,
execDir,
loggingEnabled: true,
maxUploadFileSize: 50*1024*1024,//50Мб
@@ -48,13 +44,13 @@ module.exports = {
servers: [
{
serverName: '1',
mode: 'normal', //'none', 'normal', 'site', 'reader', 'omnireader', 'liberama.top', 'book_update_checker'
mode: 'reader', //'reader', 'omnireader', 'liberama.top', 'book_update_checker'
ip: '0.0.0.0',
port: '33080',
},
/*{
serverName: '2',
mode: 'book_update_checker', //'none', 'normal', 'site', 'reader', 'omnireader', 'liberama.top', 'book_update_checker'
mode: 'book_update_checker',
isHttps: true,
keysFile: 'server',
ip: '0.0.0.0',

View File

@@ -1,4 +1,5 @@
const _ = require('lodash');
const path = require('path');
const fs = require('fs-extra');
const branchFilename = __dirname + '/application_env';
@@ -29,7 +30,7 @@ class ConfigManager {
return instance;
}
async init() {
async init(dataDir) {
if (this.inited)
throw new Error('already inited');
@@ -44,10 +45,17 @@ class ConfigManager {
process.env.NODE_ENV = this.branch;
this.branchConfigFile = __dirname + `/${this.branch}.js`;
this._config = require(this.branchConfigFile);
const config = require(this.branchConfigFile);
await fs.ensureDir(this._config.dataDir);
this._userConfigFile = `${this._config.dataDir}/config.json`;
if (dataDir) {
config.dataDir = path.resolve(dataDir);
} else {
config.dataDir = `${config.execDir}/.${config.name}`;
}
await fs.ensureDir(config.dataDir);
this._userConfigFile = `${config.dataDir}/config.json`;
this._config = config;
this.inited = true;
}
@@ -72,15 +80,28 @@ class ConfigManager {
}
async load() {
if (!this.inited)
throw new Error('not inited');
if (!await fs.pathExists(this.userConfigFile)) {
await this.save();
return;
}
try {
if (!this.inited)
throw new Error('not inited');
const data = await fs.readFile(this.userConfigFile, 'utf8');
this.config = JSON.parse(data);
if (await fs.pathExists(this.userConfigFile)) {
const data = JSON.parse(await fs.readFile(this.userConfigFile, 'utf8'));
const config = _.pick(data, propsToSave);
this.config = config;
//сохраним конфиг, если не все атрибуты присутствуют в файле конфига
for (const prop of propsToSave)
if (!Object.prototype.hasOwnProperty.call(config, prop)) {
await this.save();
break;
}
} else {
await this.save();
}
} catch(e) {
throw new Error(`Error while loading "${this.userConfigFile}": ${e.message}`);
}
}
async save() {

View File

@@ -2,15 +2,11 @@ const path = require('path');
const base = require('./base');
const execDir = path.dirname(process.execPath);
const dataDir = `${execDir}/data`;
module.exports = Object.assign({}, base, {
branch: 'production',
dataDir: dataDir,
tempDir: `${dataDir}/tmp`,
logDir: `${dataDir}/log`,
publicDir: `${execDir}/public`,
uploadDir: `${execDir}/public/upload`,
execDir,
servers: [
{