forked from billiegoose/cors-buster
-
Notifications
You must be signed in to change notification settings - Fork 38
/
bin.js
executable file
·57 lines (55 loc) · 1.28 KB
/
bin.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const {spawn} = require('child_process')
const kill = require('tree-kill')
const minimisted = require('minimisted')
async function main({_: [cmd], p, d}) {
switch (cmd) {
case 'start': {
if (d) require('daemonize-process')()
const cmd = require.resolve('micro/bin/micro.js')
const args = [
cmd,
`--listen=tcp://0.0.0.0:${p || 9999}`
]
let server = spawn(
'node', args,
{
stdio: 'inherit',
windowsHide: true,
cwd: __dirname
}
)
fs.writeFileSync(
path.join(process.cwd(), 'cors-proxy.pid'),
String(process.pid),
'utf8'
)
process.on('exit', server.kill)
return
}
case 'stop': {
let pid
try {
pid = fs.readFileSync(
path.join(process.cwd(), 'cors-proxy.pid'),
'utf8'
);
} catch (err) {
console.log('No cors-proxy.pid file')
return
}
pid = parseInt(pid)
console.log('killing', pid)
kill(pid, (err) => {
if (err) {
console.log(err)
} else {
fs.unlinkSync(path.join(process.cwd(), 'cors-proxy.pid'))
}
})
}
}
}
minimisted(main)