index.js 3.1 KB

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