UNPKG

6.55 kBJavaScriptView Raw
1/**
2 * Copyright 2013-2021 the PM2 project authors. All rights reserved.
3 * Use of this source code is governed by a license that
4 * can be found in the LICENSE file.
5 */
6
7var Configuration = module.exports = {};
8
9var fs = require('fs');
10
11var Common = require('./Common');
12var eachSeries = require('async/eachSeries');
13var cst = require('../constants.js');
14
15function splitKey(key) {
16 var values = [key];
17
18 if (key.indexOf('.') > -1)
19 values = key.match(/(?:[^."]+|"[^"]*")+/g).map(function(dt) { return dt.replace(/"/g, '') });
20 else if (key.indexOf(':') > -1)
21 values = key.match(/(?:[^:"]+|"[^"]*")+/g).map(function(dt) { return dt.replace(/"/g, '') });
22
23 return values;
24}
25
26function serializeConfiguration(json_conf) {
27 return JSON.stringify(json_conf, null, 4)
28}
29
30Configuration.set = function(key, value, cb) {
31 fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
32 if (err) return cb(err);
33
34 var json_conf = JSON.parse(data);
35
36 var values = splitKey(key);
37
38 if (values.length > 0) {
39 var levels = values;
40
41 var tmp = json_conf;
42
43 levels.forEach(function(key, index) {
44 if (index == levels.length -1)
45 tmp[key] = value;
46 else if (!tmp[key]) {
47 tmp[key] = {};
48 tmp = tmp[key];
49 }
50 else {
51 if (typeof(tmp[key]) != 'object')
52 tmp[key] = {};
53 tmp = tmp[key];
54 }
55 });
56
57 }
58 else {
59 if (json_conf[key] && typeof(json_conf[key]) === 'string')
60 Common.printOut(cst.PREFIX_MSG + 'Replacing current value key %s by %s', key, value);
61
62 json_conf[key] = value;
63 }
64
65 fs.writeFile(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf), function(err, data) {
66 if (err) return cb(err);
67
68 return cb(null, json_conf);
69 });
70 return false;
71 });
72};
73
74Configuration.unset = function(key, cb) {
75 fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
76 if (err) return cb(err);
77
78 var json_conf = JSON.parse(data);
79
80 var values = splitKey(key);
81
82 if (values.length > 0) {
83 var levels = values;
84
85 var tmp = json_conf;
86
87 levels.forEach(function(key, index) {
88 if (index == levels.length -1)
89 delete tmp[key];
90 else if (!tmp[key]) {
91 tmp[key] = {};
92 tmp = tmp[key];
93 }
94 else {
95 if (typeof(tmp[key]) != 'object')
96 tmp[key] = {};
97 tmp = tmp[key];
98 }
99 });
100
101 }
102 else
103 delete json_conf[key];
104
105 if (err) return cb(err);
106
107 if (key === 'all')
108 json_conf = {};
109
110 fs.writeFile(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf), function(err, data) {
111 if (err) return cb(err);
112
113 return cb(null, json_conf);
114 });
115 return false;
116 });
117}
118
119Configuration.setSyncIfNotExist = function(key, value) {
120 try {
121 var conf = JSON.parse(fs.readFileSync(cst.PM2_MODULE_CONF_FILE));
122 } catch(e) {
123 return null;
124 }
125
126 var values = splitKey(key);
127 var exists = false;
128
129 if (values.length > 1 && conf && conf[values[0]]) {
130 exists = Object.keys(conf[values[0]]).some(function(key) {
131 if (key == values[1])
132 return true;
133 return false;
134 });
135 }
136
137 if (exists === false)
138 return Configuration.setSync(key, value);
139
140 return null;
141};
142
143Configuration.setSync = function(key, value) {
144 try {
145 var data = fs.readFileSync(cst.PM2_MODULE_CONF_FILE);
146 } catch(e) {
147 return null;
148 }
149
150 var json_conf = JSON.parse(data);
151
152 var values = splitKey(key);
153
154 if (values.length > 0) {
155 var levels = values;
156
157 var tmp = json_conf;
158
159 levels.forEach(function(key, index) {
160 if (index == levels.length -1)
161 tmp[key] = value;
162 else if (!tmp[key]) {
163 tmp[key] = {};
164 tmp = tmp[key];
165 }
166 else {
167 if (typeof(tmp[key]) != 'object')
168 tmp[key] = {};
169 tmp = tmp[key];
170 }
171 });
172
173 }
174 else {
175 if (json_conf[key] && typeof(json_conf[key]) === 'string')
176 Common.printOut(cst.PREFIX_MSG + 'Replacing current value key %s by %s', key, value);
177
178 json_conf[key] = value;
179 }
180
181 if (key === 'all')
182 json_conf = {};
183
184 try {
185 fs.writeFileSync(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf));
186 return json_conf;
187 } catch(e) {
188 console.error(e.message);
189 return null;
190 }
191};
192
193Configuration.unsetSync = function(key) {
194 try {
195 var data = fs.readFileSync(cst.PM2_MODULE_CONF_FILE);
196 } catch(e) {
197 return null;
198 }
199
200 var json_conf = JSON.parse(data);
201
202 var values = splitKey(key);
203
204 if (values.length > 0) {
205 var levels = values;
206
207 var tmp = json_conf;
208
209 levels.forEach(function(key, index) {
210 if (index == levels.length -1)
211 delete tmp[key];
212 else if (!tmp[key]) {
213 tmp[key] = {};
214 tmp = tmp[key];
215 }
216 else {
217 if (typeof(tmp[key]) != 'object')
218 tmp[key] = {};
219 tmp = tmp[key];
220 }
221 });
222
223 }
224 else
225 delete json_conf[key];
226
227 if (key === 'all')
228 json_conf = {};
229
230 try {
231 fs.writeFileSync(cst.PM2_MODULE_CONF_FILE, serializeConfiguration(json_conf));
232 } catch(e) {
233 console.error(e.message);
234 return null;
235 }
236};
237
238Configuration.multiset = function(serial, cb) {
239 var arrays = [];
240 serial = serial.match(/(?:[^ "]+|"[^"]*")+/g);
241
242 while (serial.length > 0)
243 arrays.push(serial.splice(0, 2));
244
245 eachSeries(arrays, function(el, next) {
246 Configuration.set(el[0], el[1], next);
247 }, cb);
248};
249
250Configuration.get = function(key, cb) {
251 Configuration.getAll(function(err, data) {
252 var climb = splitKey(key);
253
254 climb.some(function(val) {
255 if (!data[val]) {
256 data = null;
257 return true;
258 }
259 data = data[val];
260 return false;
261 });
262
263 if (!data) return cb({err : 'Unknown key'}, null);
264 return cb(null, data);
265 });
266};
267
268Configuration.getSync = function(key) {
269 try {
270 var data = Configuration.getAllSync();
271 } catch(e) {
272 return null;
273 }
274
275 var climb = splitKey(key);
276
277 climb.some(function(val) {
278 if (!data[val]) {
279 data = null;
280 return true;
281 }
282 data = data[val];
283 return false;
284 });
285
286 if (!data) return null;
287 return data;
288};
289
290Configuration.getAll = function(cb) {
291 fs.readFile(cst.PM2_MODULE_CONF_FILE, function(err, data) {
292 if (err) return cb(err);
293 return cb(null, JSON.parse(data));
294 });
295};
296
297Configuration.getAllSync = function() {
298 try {
299 return JSON.parse(fs.readFileSync(cst.PM2_MODULE_CONF_FILE));
300 } catch(e) {
301 console.error(e.stack || e);
302 return {};
303 }
304};