UNPKG

@appium/docutils

Version:

Documentation generation utilities for Appium and related projects

43 lines 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.spawnBackgroundProcess = spawnBackgroundProcess; exports.execWithErrorHandling = execWithErrorHandling; const node_child_process_1 = require("node:child_process"); const teen_process_1 = require("teen_process"); /** * Spawns a long-running "background" child process. This is expected to only return control to the * parent process in the case of a nonzero exit code from the child process. * @param command Command to run * @param args Args to pass to command * @param opts Spawn options (`stdio` is always set to `'inherit'`) * @privateRemarks `teen_process` is good for running a one-shot command, but not so great for * background tasks; we use node's `child_process` directly here to pass `stdio` through, since * `teen_process` basically does not respect `{stdio: 'inherit'}`. */ async function spawnBackgroundProcess(command, args, opts = {}) { return new Promise((resolve, reject) => { (0, node_child_process_1.spawn)(command, args, { ...opts, stdio: 'inherit' }) .on('error', reject) .on('close', (code, signal) => { if (code === 0) { return resolve(); } const reason = code === null ? `signal ${signal ?? 'unknown'}` : `code ${code}`; reject(new Error(`${command} exited with ${reason}`)); }); }); } /** * Wraps {@linkcode exec} with error handling that appends stderr to the thrown error message. */ async function execWithErrorHandling(cmd, args, opts) { try { return await (0, teen_process_1.exec)(cmd, args, opts); } catch (err) { const execErr = err; execErr.message = execErr.stderr ? `${execErr.message}\nCommand error:\n${execErr.stderr}` : execErr.message; throw execErr; } } //# sourceMappingURL=process.js.map