UNPKG

@figliolia/child-process

Version:

A wrapper around the node.js spawn function providing a promise and graceful exiting

50 lines (49 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChildProcess = void 0; const node_child_process_1 = require("node:child_process"); const node_util_1 = require("node:util"); class ChildProcess { constructor(command, options = { stdio: "inherit" }) { const [root, args] = ChildProcess.split(command); this.process = (0, node_child_process_1.spawn)(root, args, options); this.handler = ChildProcess.promisify(this.process); } static wrapCommand(command, options = { stdio: "inherit" }) { const [root, args] = this.split(command); const CP = (0, node_child_process_1.spawn)(root, args, options); return this.promisify(CP); } static promisify(CP) { return new Promise((resolve, reject) => { CP.on("exit", code => { if (code === 1) { reject(); } else { resolve(); } }); }); } static split(command) { const [root, ...args] = command.split(" "); return [root, args]; } static get parentProcess() { return process; } static bindExits(CPs) { const NUKE = () => { CPs.forEach(CP => CP.kill()); }; this.parentProcess.on("exit", NUKE); this.parentProcess.on("SIGINT", NUKE); this.parentProcess.on("SIGQUIT", NUKE); this.parentProcess.on("beforeExit", NUKE); this.parentProcess.on("uncaughtException", NUKE); this.parentProcess.on("unhandledRejection", NUKE); } } exports.ChildProcess = ChildProcess; ChildProcess.execute = (0, node_util_1.promisify)(node_child_process_1.exec);