index.js 3.1 KB

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