forked from billiegoose/cors-buster
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add 'cors-proxy' CLI that can launch proxy in the background (#3)
- Loading branch information
1 parent
6e84faa
commit e77f060
Showing
4 changed files
with
198 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/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 = path.join( | ||
process.cwd(), | ||
'node_modules', | ||
'.bin', | ||
process.platform === 'win32' ? 'micro.cmd' : 'micro' | ||
) | ||
const args = [ | ||
`--listen=tcp://0.0.0.0:${p || 9999}` | ||
] | ||
let server = spawn( | ||
cmd, args, | ||
{ | ||
stdio: 'inherit', | ||
windowsHide: true | ||
} | ||
) | ||
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) |
Oops, something went wrong.