-
Notifications
You must be signed in to change notification settings - Fork 12
/
setapp.js
53 lines (48 loc) · 1.42 KB
/
setapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var appFileFolder = "./configs/";
var appFilePath = appFileFolder + process.env.app + ".json";
var fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
function pm2StartParseServer(appName) {
return new Promise(async function(res,rej) {
const { stdout, stderr } = await exec('pm2 start appServers.json --only '+appName);
if(stderr) {
console.log('stderr:', stderr);
rej();
} else {
console.log('stdout:', stdout);
res();
}
});
}
function writeJson(appProfile){
//get and increase port number
return new Promise(function(res,rej) {
fs.writeFile(appFilePath,
JSON.stringify(appProfile), function (err,data) {
if (err) {
throw new Error(err);
}
res();
});
});
}
let config = {};
if(!process.env.app) {
console.log('app is not defined!');
return false;
} else {
try{
config = require('./configs/'+process.env.app);
} catch(err) {
console.log('app is not found!');
return false;
}
}
config.enable = !(process.env.enable === 'false');
writeJson(config).then(function () {
return pm2StartParseServer(process.env.app);
}).then(function () {
let str = config.enable ? 'enabled!':'disabled!';
console.log(process.env.app+' is now '+ str);
});