This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle.js
86 lines (63 loc) · 1.93 KB
/
lifecycle.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
const shell = require('./shell');
const { spawn } = require('child_process');
const config = require('./config-cli');
const style = require('./styles');
async function checkRunning (pid) {
try {
await shell('kill', ['-0', pid]);
return true;
} catch (e) {
return false;
}
}
async function start() {
console.log(style.info('Starting Corsica...'));
let { installPath, pid } = await config.get();
if (pid) {
if (await checkRunning(pid)) {
throw style.bad('Corsica is already running!');
} else {
console.warn(style.notice('Unable match running Corsica to process id, cleaning up.'));
await config.set('pid', null);
}
}
let subprocess = shell('node', ['node_modules/corsica/index.js'], {
cwd: installPath
});
await config.set('pid', process.pid);
}
async function startInBackground() {
console.log(style.info('Starting Corsica...'));
let { installPath, pid } = await config.get();
if (pid) {
if (await checkRunning(pid)) {
throw style.bad('Corsica is already running!');
} else {
console.warn(style.notice('Unable match running Corsica to process id, cleaning up.'));
await config.set('pid', null);
}
}
let subprocess = spawn('node', ['node_modules/corsica/index.js'], {
cwd: installPath,
detached: true,
stdio: 'ignore'
});
subprocess.unref();
console.log(style.good('Corsica is now running in the background. Stop it with "corsica stop".'));
await config.set('pid', subprocess.pid);
}
async function stop() {
console.log(style.info('Stopping Corsica...'));
let { pid } = await config.get();
if (pid) {
try {
await shell('kill', [pid]);
} catch (e) {
console.warn(style.notice('Unable match running Corsica to process id, cleaning up.'));
}
await config.set('pid', null);
} else {
console.log(style.notice('No Running Corsica Found!'));
}
}
module.exports = {start, startInBackground, stop};