UNPKG

4.21 kBJavaScriptView Raw
1/**
2 * Manages the loading and saving of configuration data.
3 *
4 * Written By:
5 * Matthew Knox
6 *
7 * License:
8 * MIT License. All code unless otherwise specified is
9 * Copyright (c) Matthew Knox and Contributors 2015.
10 */
11
12var 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
20var 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
31var 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); // deliberate use of undefined, will cause property to be deleted.
35 }
36 return value;
37 }, 4), 'utf8');
38};
39
40exports.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) { } // fs.existsSync is deprecated for some unknown reason
48
49 // don't bother saving if there is no config to overwrite and no config to save
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
62exports.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
74exports.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
97exports.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
112exports.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
139exports.loadOutputConfig = function (outputName) {
140 var config = exports.getConfig(sysConfigZones[0]);
141 if (!config[outputName]) {
142 config[outputName] = {};
143 }
144 return config[outputName];
145};