forked from cevio/cpm-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·65 lines (59 loc) · 1.52 KB
/
index.js
File metadata and controls
executable file
·65 lines (59 loc) · 1.52 KB
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
#!/usr/bin/env node
const argv = process.argv.slice(2);
const fs = require('fs');
const path = require('path');
const hostFile = path.resolve(__dirname, './HOST');
let closing = 0, timer = null;
(() => {
if (!argv.length) return;
if (argv[0] === 'host') {
HOST(argv[1], true);
return process.exit(0);
}
const registry = HOST(null, false);
argv.push('--registry=' + registry);
listen();
const ls = require('child_process').spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', argv, {
cwd: process.cwd(),
stdio: 'inherit'
});
ls.on('close', () => {
if (closing === 1) {
closing = 2;
} else {
destory();
process.exit(0);
}
});
})();
function HOST(registry, log) {
if (registry) {
fs.writeFileSync(hostFile, registry, 'utf8');
return console.log(`\n 🍡 change registry to '${registry}'\n`);
}
if (!fs.existsSync(hostFile)) return console.log(`\n 💀 sorry non-registry\n`);
registry = fs.readFileSync(hostFile, 'utf8');
log && console.log(registry);
return registry;
}
function close() {
if ([1, 2].indexOf(closing) > -1) return;
closing = 1;
timer = setInterval(() => {
if (closing === 2) {
destory();
clearInterval(timer);
process.exit(0);
}
}, 10);
}
function listen() {
process.on('SIGINT', close);
process.on('SIGQUIT', close);
process.on('SIGTERM', close);
}
function destory() {
process.removeListener('SIGINT', close);
process.removeListener('SIGQUIT', close);
process.removeListener('SIGTERM', close);
}