index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const _ = require('lodash');
  2. const path = require('path');
  3. const fs = require('fs-extra');
  4. const branchFilename = __dirname + '/application_env';
  5. const propsToSave = [
  6. 'accessPassword',
  7. 'bookReadLink',
  8. 'loggingEnabled',
  9. 'dbCacheSize',
  10. 'maxFilesDirSize',
  11. 'queryCacheEnabled',
  12. 'cacheCleanInterval',
  13. 'inpxCheckInterval',
  14. 'lowMemoryMode',
  15. 'fullOptimization',
  16. 'allowRemoteLib',
  17. 'remoteLib',
  18. 'server',
  19. ];
  20. let instance = null;
  21. //singleton
  22. class ConfigManager {
  23. constructor() {
  24. if (!instance) {
  25. this.inited = false;
  26. instance = this;
  27. }
  28. return instance;
  29. }
  30. async init(dataDir) {
  31. if (this.inited)
  32. throw new Error('already inited');
  33. this.branch = 'production';
  34. try {
  35. await fs.access(branchFilename);
  36. this.branch = (await fs.readFile(branchFilename, 'utf8')).trim();
  37. } catch (err) {
  38. //
  39. }
  40. process.env.NODE_ENV = this.branch;
  41. this.branchConfigFile = __dirname + `/${this.branch}.js`;
  42. const config = require(this.branchConfigFile);
  43. if (dataDir) {
  44. config.dataDir = path.resolve(dataDir);
  45. } else {
  46. config.dataDir = `${config.execDir}/.${config.name}`;
  47. }
  48. await fs.ensureDir(config.dataDir);
  49. this._userConfigFile = `${config.dataDir}/config.json`;
  50. this._config = config;
  51. this.inited = true;
  52. }
  53. get config() {
  54. if (!this.inited)
  55. throw new Error('not inited');
  56. return _.cloneDeep(this._config);
  57. }
  58. set config(value) {
  59. Object.assign(this._config, value);
  60. }
  61. get userConfigFile() {
  62. return this._userConfigFile;
  63. }
  64. set userConfigFile(value) {
  65. if (value)
  66. this._userConfigFile = value;
  67. }
  68. async load() {
  69. try {
  70. if (!this.inited)
  71. throw new Error('not inited');
  72. if (await fs.pathExists(this.userConfigFile)) {
  73. const data = JSON.parse(await fs.readFile(this.userConfigFile, 'utf8'));
  74. const config = _.pick(data, propsToSave);
  75. this.config = config;
  76. //сохраним конфиг, если не все атрибуты присутствуют в файле конфига
  77. for (const prop of propsToSave)
  78. if (!Object.prototype.hasOwnProperty.call(config, prop)) {
  79. await this.save();
  80. break;
  81. }
  82. } else {
  83. await this.save();
  84. }
  85. } catch(e) {
  86. throw new Error(`Error while loading "${this.userConfigFile}": ${e.message}`);
  87. }
  88. }
  89. async save() {
  90. if (!this.inited)
  91. throw new Error('not inited');
  92. const dataToSave = _.pick(this._config, propsToSave);
  93. await fs.writeFile(this.userConfigFile, JSON.stringify(dataToSave, null, 4));
  94. }
  95. }
  96. module.exports = ConfigManager;