Skip to content

Commit 8c578a8

Browse files
committed
Added restart flag
1 parent 5514116 commit 8c578a8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

bin.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ const temp = tmpdir();
1212

1313
const {
1414
positionals: [cmd],
15-
values: { port, help, pid: pidFile },
15+
values: { port, help, pid: pidFile, restart },
1616
} = parseArgs({
1717
options: {
1818
help: { type: 'boolean', short: 'h', default: false },
1919
port: { type: 'string', short: 'p', default: '9999' },
2020
pid: { type: 'string', default: join(temp, 'cors-proxy.pid') },
21+
restart: { type: 'boolean', short: 'r', default: false },
2122
},
2223
allowPositionals: true,
2324
});
@@ -33,7 +34,8 @@ Commands:
3334
Options:
3435
-h, --help Show this help message
3536
-p, --port <port> Port to listen on (default: 9999)
36-
--pid <path> Path to PID file (default: ${join(temp, 'cors-proxy.pid')}`);
37+
--pid <path> Path to PID file (default: ${join(temp, 'cors-proxy.pid')}
38+
-r, --restart Restart the daemon if it is already running. Only valid with start`);
3739
}
3840

3941
function getPID(strict) {
@@ -73,12 +75,15 @@ switch (cmd) {
7375
if (existsSync(pidFile)) {
7476
const [pid, processExists] = getPID(true);
7577

76-
if (processExists) {
77-
console.error(`Daemon is already running (pid is ${pid})`);
78-
process.exit(16);
79-
} else {
78+
if (!processExists) {
8079
unlinkSync(pidFile);
8180
console.error('Removed stale PID file');
81+
} else if (restart) {
82+
process.kill(pid);
83+
console.error('Stopped existing daemon');
84+
} else {
85+
console.error(`Daemon is already running (pid is ${pid})`);
86+
process.exit(16);
8287
}
8388
}
8489

@@ -88,7 +93,7 @@ switch (cmd) {
8893
{ stdio: 'ignore', detached: true },
8994
);
9095
daemon.unref();
91-
console.log('Started CORS proxy server with PID', daemon.pid);
96+
console.log('Started CORS proxy daemon with PID', daemon.pid);
9297
writeFileSync(pidFile, daemon.pid.toString());
9398
break;
9499
}

0 commit comments

Comments
 (0)