UNPKG

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