@unito/integration-debugger
Version:
The Unito Integration Debugger
61 lines (60 loc) • 2.25 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.spawnProcess = spawnProcess;
const child_process_1 = require("child_process");
function spawnProcess(commandLine, dataCallback) {
const [command, ...args] = commandLine.split(' ');
// We start the process with "detached: true" so it creates a "process group".
//
// In a POSIX-conformant operating system, a process group denotes a collection
// of one or more processes. Among other things, a process group is used to
// control the distribution of a signal; when a signal is directed to a process group,
// the signal is delivered to each process that is a member of the group.
//
const childProcess = (0, child_process_1.spawn)(command, args, {
detached: true,
env: { ...process.env, NODE_ENV: 'development' },
});
childProcess.stdout.setEncoding('utf8');
childProcess.stdout.on('data', function (data) {
dataCallback(data.toString());
});
childProcess.stderr.setEncoding('utf8');
childProcess.stderr.on('data', function (data) {
dataCallback(data.toString());
});
childProcess.on('close', function (code) {
dataCallback(`Server closed (code: ${code})`);
});
// Kill the integration process when the main process is killed externally.
//
// The "-" before the PID is used to send the signal to the "process group".
['SIGTERM', 'SIGINT', 'SIGUSR2'].forEach(signalType => {
process.on(signalType, function () {
// istanbul ignore next
if (childProcess.pid) {
try {
process.kill(-childProcess.pid);
}
catch {
/* NOOP */
}
}
});
});
// Kill the integration process when the main process ends.
//
// The "-" before the PID is used to send the signal to the "process group".
process.on('exit', function () {
// istanbul ignore next
if (childProcess.pid) {
try {
process.kill(-childProcess.pid);
}
catch {
/* NOOP */
}
}
});
return childProcess;
}
;