nx
Version:
95 lines (94 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EXPECTED_TERMINATION_SIGNALS = void 0;
exports.signalToCode = signalToCode;
exports.codeToSignal = codeToSignal;
exports.messageToCode = messageToCode;
exports.exitAsInterrupted = exitAsInterrupted;
/**
* Translates NodeJS signals to numeric exit code
* @param signal
*/
function signalToCode(signal) {
switch (signal) {
case 'SIGHUP':
return 128 + 1;
case 'SIGINT':
return 128 + 2;
case 'SIGTERM':
return 128 + 15;
case 'SIGQUIT':
return 128 + 3;
default:
return 128;
}
}
/**
* Translates numeric exit codes to NodeJS signals
*/
function codeToSignal(code) {
switch (code) {
case 128 + 1:
return 'SIGHUP';
case 128 + 2:
return 'SIGINT';
case 128 + 15:
return 'SIGTERM';
case 128 + 3:
return 'SIGQUIT';
default:
return 'SIGTERM';
}
}
// Exit codes from signals that indicate intentional termination (SIGHUP, SIGINT, SIGQUIT, SIGTERM).
// Excludes SIGKILL (137) and SIGABRT (134) as those indicate abnormal termination.
exports.EXPECTED_TERMINATION_SIGNALS = new Set([129, 130, 131, 143]);
/**
* Translates a pty exit message (e.g. "Terminated by Interrupt") to a numeric exit code.
* Handles both Linux exact-match and macOS strsignal formats (e.g. "Terminated by Hangup: 1").
*/
function messageToCode(message) {
if (message.startsWith('Terminated by ')) {
const signalDescription = message.replace('Terminated by ', '').trim();
if (signalDescription.startsWith('Hangup'))
return 129;
if (signalDescription.startsWith('Interrupt'))
return 130;
if (signalDescription.startsWith('Quit'))
return 131;
if (signalDescription.startsWith('Abort'))
return 134;
if (signalDescription.startsWith('Killed'))
return 137;
if (signalDescription.startsWith('Terminated'))
return 143;
return 128;
}
else if (message.startsWith('Exited with code ')) {
return parseInt(message.replace('Exited with code ', '').trim());
}
else if (message === 'Success') {
return 0;
}
else {
return 1;
}
}
/**
* Ends the process the way an interrupted process ends: by SIGINT, not by
* exiting with its conventional code.
*
* A shell reports 130 either way, but a parent using waitpid sees
* `signal: 'SIGINT'` rather than `code: 130`, which is what tools wrapping nx
* check to tell "the user pressed Ctrl+C" from "the command failed".
*/
function exitAsInterrupted() {
// Windows has no signal disposition to inherit - `process.kill` there
// terminates with an arbitrary code - so keep the 128+SIGINT convention.
if (process.platform !== 'win32') {
// Any listener would swallow this; the default action is what terminates.
process.removeAllListeners('SIGINT');
process.kill(process.pid, 'SIGINT');
}
process.exit(130);
}