index.js 3.1 KB

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