index.js 2.9 KB

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