-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
262 lines (225 loc) · 6.98 KB
/
cli.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var util = require('util');
var comUtil = require('./util');
var path = require('path');
var fs = require('fs');
var fse = require('fs-extra2');
var cp = require('child_process');
var os = require('os');
var BOOTSTRAP_PATH = path.join(__dirname, 'bootstrap.js');
//默认设置为`~`,防止Linux在开机启动时Node无法获取homedir
var TIMEOUT = 1200;
var defaultDir;
var dataDir;
function getHomeDir() {
return (typeof os.homedir == 'function' ? os.homedir() : process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME']) || '~';
}
function getDataDir() {
if (!dataDir) {
var homedir = process.env.STARTING_DATA_DIR || getHomeDir();
dataDir = path.join(homedir, '.startingAppData');
fse.ensureDirSync(dataDir);
}
return dataDir;
}
exports.getDefaultDir = function() {
defaultDir = defaultDir || path.join(getHomeDir(), '.startingAppData');
return defaultDir;
};
/* eslint-disable no-console */
function isRunning(pid, callback) {
pid ? cp.exec(util.format(process.platform === 'win32' ?
'tasklist /fi "PID eq %s" | findstr /i "node.exe"'
: 'ps -f -p %s | grep "node"', pid),
function (err, stdout, stderr) {
callback(!err && !!stdout.toString().trim());
}) : callback(false);
}
exports.isRunning = isRunning;
function run(main, options, callback) {
options.debugMode = true;
callback = callback || noop;
var child = execCmd(main, options, {
stdio: [ 0, 1, 2]
});
var execCallback = function() {
timer && callback(null, options);
timer = null;
};
var timer = setTimeout(execCallback, TIMEOUT);
var handleExit = function() {
clearTimeout(timer);
timer = null;
};
child.on('exit', handleExit);
child.on('close', handleExit);
}
exports.run = run;
function start(main, options, callback, version, isRestart) {
var config = readConfig(main);
if (config && !isRestart && config.version != version) {
return restart(main, options, callback, version);
}
callback = callback || noop;
isRunning(config.pid, function (isrunning) {
if (isrunning) {
return callback(true, config.options || {});
}
isRunning(config._pid, function (isrunning) {
if (isrunning) {
return callback(true, config.options || {});
}
var errorFile = path.join(getDataDir(), 'error.' + encodeURIComponent(main));
try {
if (fs.existsSync(errorFile)) {
fs.unlinkSync(errorFile);
}
} catch(e) {
return callback(e, options);
}
var child = execCmd(main, options, {
detached: true,
stdio: ['ignore', 'ignore', fs.openSync(errorFile, 'a+')]
});
config._pid = child.pid;
config.main = main;
config.version = version;
try {
writeConfig(main, config);
} catch(e) {
return callback(e, options);
}
var startTime = Date.now();
(function execCallback() {
var error;
try {
error = fs.readFileSync(errorFile, {encoding: 'utf8'});
} catch(e) {}
if (error) {
callback(null, options);
console.error(error);
try {
fs.unlinkSync(errorFile);
} catch(e) {}
return;
}
if (Date.now() - startTime < TIMEOUT) {
return setTimeout(execCallback, 200);
}
delete config._pid;
config.pid = child.pid;
config.options = options;
writeConfig(main, config);
child.unref();
callback(null, options);
})();
});
});
}
exports.start = start;
function restart(main, options, callback, version) {
stop(main, function (_, _main, opts) {
setTimeout(function () {
if (!options.clearPreOptions) {
var noGlobalPlugins = opts.noGlobalPlugins;
options = Object.assign(opts, options);
options.noGlobalPlugins = noGlobalPlugins || options.noGlobalPlugins;
}
start(main, options, callback, version, true);
}, 300);
});
}
exports.restart = restart;
function stop(main, callback) {
var config = readConfig(main);
var pid = config.pid;
var _pid = config._pid;
main = config.main;
callback = callback || noop;
isRunning(pid, function (isrunning) {
try {
pid && process.kill(pid);
removeConfig(main);
} catch (err) {
isrunning = isrunning && err;
}
if (isrunning) {
callback(isrunning, main, config.options || {});
} else {
isRunning(_pid, function (isrunning) {
try {
_pid && process.kill(_pid);
removeConfig(main);
} catch (err) {
isrunning = isrunning && err;
}
callback(isrunning, main, config.options || {});
});
}
});
}
exports.stop = stop;
function getHashIndex(main) {
var lastIndex = main.length - 1;
if (main[lastIndex] == '#') {
lastIndex = main.lastIndexOf('#', lastIndex - 1);
} else {
lastIndex = -1;
}
return lastIndex;
}
function getRunningPath(main, dataDir) {
var index = getHashIndex(main);
main = encodeURIComponent(index == -1 ? '#' : main.substring(index));
return path.join(dataDir || getDataDir(), main);
}
function execCmd(main, data, options) {
var lastIndex = getHashIndex(main);
if (lastIndex != -1) {
main = main.substring(0, lastIndex);
}
var args = [BOOTSTRAP_PATH, 'run', main];
if (data.inspectBrk) {
args.unshift('--inspect-brk' + (data.inspectBrk === true ? '' : '=' + data.inspectBrk));
} else if (data.inspect) {
args.unshift('--inspect' + (data.inspect === true ? '' : '=' + data.inspect));
}
var execPath = process.env.STARTING_EXEC_PATH;
var version = comUtil.getVersion(execPath);
if (version) {
var flag = comUtil.getMaxSemiSpaceFlag(version);
flag && args.unshift(flag + '=64');
version = version.substring(1).split('.');
var supportTlsMinV1 = version[0] > 11;
var supportMaxHeaderSize = (version[0] == 10 && version[1] >= 15) || (version[0] == 11 && version[1] > 5) || version[0] > 11;
var maxHeaderSize = data.__maxHttpHeaderSize;
supportMaxHeaderSize && args.unshift('--max-http-header-size=' + (maxHeaderSize > 0 ? maxHeaderSize : 256000));
supportTlsMinV1 && args.unshift('--tls-min-v1.0');
}
if (data) {
args.push('--data');
args.push(encodeURIComponent(JSON.stringify(data)));
}
return cp.spawn(execPath || 'node', args, options);
}
function readConfig(main, dataDir) {
try {
return fse.readJsonSync(getRunningPath(main, dataDir));
} catch(e) {}
var running = path.join(dataDir || getDataDir(), encodeURIComponent(main));
try {
return fse.readJsonSync(running);
} catch(e) {}
return {};
}
exports.readConfig = readConfig;
function writeConfig(main, config) {
fse.outputJsonSync(getRunningPath(main), config || {});
}
function removeConfig(main, dataDir) {
var running = getRunningPath(main, dataDir);
fs.existsSync(running) && fse.removeSync(running);
running = path.join(dataDir || getDataDir(), encodeURIComponent(main));
fs.existsSync(running) && fse.removeSync(running);
}
exports.removeConfig = removeConfig;
function noop() {}