1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var fs = require('fs'),
|
13 | path = require('path'),
|
14 | modConfig = null,
|
15 | modConfigFile = 'config.json',
|
16 | sysConfig = null,
|
17 | sysConfigZones = ['output', 'disabled', 'update', 'admin', 'i18n', 'firstRun', 'loopback'],
|
18 | sysConfigFile = 'config.json';
|
19 |
|
20 | var loadConfig = function (location) {
|
21 | try {
|
22 | var data = fs.readFileSync(location, 'utf8');
|
23 | return JSON.parse(data);
|
24 | }
|
25 | catch (e) {
|
26 | console.debug($$`No or invalid configuration file found at "${location}".`);
|
27 | return {};
|
28 | }
|
29 | };
|
30 |
|
31 | var saveIndividualConfig = function (location, data) {
|
32 | fs.writeFileSync(location, JSON.stringify(data, function(key, value) {
|
33 | if (sysConfigZones.includes(key) && Object.keys(value).length === 0) {
|
34 | return void (0);
|
35 | }
|
36 | return value;
|
37 | }, 4), 'utf8');
|
38 | };
|
39 |
|
40 | exports.saveModuleConfig = function(mod) {
|
41 | try {
|
42 | var m = modConfig[mod];
|
43 | var exists = false;
|
44 | try {
|
45 | fs.statSync(m.location);
|
46 | exists = true;
|
47 | } catch (e) { }
|
48 |
|
49 |
|
50 | if (Object.keys(m).length !== 0 || exists) {
|
51 | saveIndividualConfig(m.location, m.data);
|
52 | }
|
53 | delete modConfig[mod];
|
54 | return true;
|
55 | } catch (e) {
|
56 | console.error($$`An error occured while saving the configuration file.`);
|
57 | console.critical(e);
|
58 | return false;
|
59 | }
|
60 | };
|
61 |
|
62 | exports.saveSystemConfig = function () {
|
63 | try {
|
64 | saveIndividualConfig(sysConfigFile, sysConfig);
|
65 | sysConfig = null;
|
66 | return true;
|
67 | } catch (e) {
|
68 | console.error($$`An error occured while saving the configuration file.`);
|
69 | console.critical(e);
|
70 | return false;
|
71 | }
|
72 | };
|
73 |
|
74 | exports.getConfig = function (m) {
|
75 | var isSystem = sysConfigZones.includes(m);
|
76 |
|
77 | if (!sysConfig) {
|
78 | sysConfig = loadConfig(sysConfigFile);
|
79 | }
|
80 |
|
81 | if (!isSystem && !sysConfig[m]) {
|
82 | return {};
|
83 | }
|
84 | else if (!sysConfig[m]) {
|
85 | sysConfig[m] = {};
|
86 | }
|
87 |
|
88 | if (!isSystem) {
|
89 | console.warn($$`Configuration data for module ${m} stored in deprecated location.`);
|
90 | var cfg = sysConfig[m];
|
91 | delete sysConfig[m];
|
92 | return cfg;
|
93 | }
|
94 | return sysConfig[m];
|
95 | };
|
96 |
|
97 | exports.getLoadedModuleConfig = function (moduleName) {
|
98 | if (!modConfig) {
|
99 | modConfig = {};
|
100 | }
|
101 |
|
102 | if (!modConfig[moduleName]) {
|
103 | let location = path.join(global.__modulesPath, moduleName, modConfigFile);
|
104 | modConfig[moduleName] = {
|
105 | location: location,
|
106 | data: loadConfig(location)
|
107 | };
|
108 | }
|
109 | return modConfig[moduleName];
|
110 | };
|
111 |
|
112 | exports.loadModuleConfig = function (module, location, ignoreCache) {
|
113 | if (!modConfig) {
|
114 | modConfig = {};
|
115 | }
|
116 |
|
117 | if (modConfig[module.name] && !ignoreCache) {
|
118 | return modConfig[module.name];
|
119 | }
|
120 |
|
121 | var loc = path.join(location, modConfigFile),
|
122 | configData = loadConfig(loc),
|
123 | deprecatedDataA = exports.getConfig(module.name),
|
124 | deprecatedDataB = exports.getConfig(module.name + '.js');
|
125 | for (var name in deprecatedDataA) {
|
126 | configData[name] = deprecatedDataA[name];
|
127 | }
|
128 | for (var name in deprecatedDataB) {
|
129 | configData[name] = deprecatedDataB[name];
|
130 | }
|
131 |
|
132 | modConfig[module.name] = {
|
133 | location: path.join(location, modConfigFile),
|
134 | data: configData
|
135 | };
|
136 | return configData;
|
137 | };
|
138 |
|
139 | exports.loadOutputConfig = function (outputName) {
|
140 | var config = exports.getConfig(sysConfigZones[0]);
|
141 | if (!config[outputName]) {
|
142 | config[outputName] = {};
|
143 | }
|
144 | return config[outputName];
|
145 | };
|