index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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._restrictedFile = `${config.dataDir}/restricted.json`;
  47. this._config = config;
  48. this.inited = true;
  49. }
  50. get config() {
  51. if (!this.inited)
  52. throw new Error('not inited');
  53. return _.cloneDeep(this._config);
  54. }
  55. set config(value) {
  56. Object.assign(this._config, value);
  57. }
  58. get userConfigFile() {
  59. return this._userConfigFile;
  60. }
  61. get restrictedFile() {
  62. return this._restrictedFile;
  63. }
  64. set userConfigFile(value) {
  65. if (value)
  66. this._userConfigFile = value;
  67. }
  68. async load() {
  69. try {
  70. if (!this.inited)
  71. throw new Error('not inited');
  72. if (await fs.pathExists(this.userConfigFile)) {
  73. const data = JSON.parse(await fs.readFile(this.userConfigFile, 'utf8'));
  74. const config = _.pick(data, propsToSave);
  75. this.config = config;
  76. //сохраним конфиг, если не все атрибуты присутствуют в файле конфига
  77. for (const prop of propsToSave)
  78. if (!Object.prototype.hasOwnProperty.call(config, prop)) {
  79. await this.save();
  80. break;
  81. }
  82. } else {
  83. await this.save();
  84. }
  85. if (await fs.pathExists(this.restrictedFile)) {
  86. const data = JSON.parse(await fs.readFile(this.restrictedFile, 'utf8'));
  87. this.config = {restricted: data};
  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;