index.js 3.0 KB

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