index.js 3.0 KB

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