Skip to content

Commit

Permalink
feat: add keepalive args (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored May 16, 2024
1 parent 2d513f2 commit 4b82f4f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ $ npx mwtsc --watch --run ./bootstrap.js

# run with tsc options
$ npx mwtsc --watch --project tsconfig.production.json --run ./bootstrap.js

# the child process keep avaliable during the development
$ npx mwtsc --watch --run ./bootstrap.js --keepalive
```

## Different with tsc and tsc-watch
Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.CHILD_PROCESS_EXCEPTION_EXIT_CODE = 10;
23 changes: 22 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { fork, spawn } = require('child_process');
const { filterFileChangedText, debug } = require('./util');
const { filterFileChangedText, debug, output, colors } = require('./util');
const { CHILD_PROCESS_EXCEPTION_EXIT_CODE } = require('./constants');
// is windows
const isWin = process.platform === 'win32';

Expand Down Expand Up @@ -110,6 +111,26 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => {
}
};
runChild.on('message', onServerReady);
if (runArgs.includes('--keepalive')) {
runChild.once('exit', code => {
if (code === CHILD_PROCESS_EXCEPTION_EXIT_CODE) {
output(colors.red('*'.repeat(120)));
output(
`A ${colors.red(
`${colors.bright(
'Critical unhandledRejection or uncaughtException'
)}`
)} was detected and the process has exited automatically.`
);
output('Please make sure to handle it.');
output(
'The --keepalive parameter was enabled, we will do our best to ensure the process remains normal..'
);
output(colors.red('*'.repeat(120)));
innerFork(false);
}
});
}
}

innerFork(true);
Expand Down
15 changes: 15 additions & 0 deletions lib/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
const childPath = process.env.CHILD_CMD_PATH;
const childCwd = process.env.CHILD_CWD;
const { join, isAbsolute } = require('path');
const { CHILD_PROCESS_EXCEPTION_EXIT_CODE } = require('./constants');

// get the child process execArgs
const runArgs = process.argv.slice(2);
if (runArgs.includes('--keepalive')) {
process.once('unhandledRejection', err => {
console.error(err);
process.exit(CHILD_PROCESS_EXCEPTION_EXIT_CODE);
});

process.once('uncaughtException', err => {
console.error(err);
process.exit(CHILD_PROCESS_EXCEPTION_EXIT_CODE);
});
}

process.on('message', data => {
if (data.title === 'server-kill') {
Expand Down

0 comments on commit 4b82f4f

Please sign in to comment.