Skip to content

Commit

Permalink
feat(child-process): Add JSDoc types
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Dec 10, 2020
1 parent 556056a commit 1840492
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core/child-process/index.js
Expand Up @@ -6,6 +6,7 @@ const execa = require("execa");
const logTransformer = require("strong-log-transformer");

// bookkeeping for spawned processes
/** @type {Set<import("execa").ExecaChildProcess<string>>} */
const children = new Set();

// when streaming processes are spawned, use this color for prefix
Expand All @@ -15,24 +16,49 @@ const NUM_COLORS = colorWheel.length;
// ever-increasing index ensures colors are always sequential
let currentColor = 0;

/**
* Execute a command asynchronously, piping stdio by default.
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} [opts]
*/
function exec(command, args, opts) {
const options = Object.assign({ stdio: "pipe" }, opts);
const spawned = spawnProcess(command, args, options);

return wrapError(spawned);
}

/**
* Execute a command synchronously.
* @param {string} command
* @param {string[]} args
* @param {import("execa").SyncOptions} [opts]
*/
function execSync(command, args, opts) {
return execa.sync(command, args, opts).stdout;
}

/**
* Spawn a command asynchronously, _always_ inheriting stdio.
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} [opts]
*/
function spawn(command, args, opts) {
const options = Object.assign({}, opts, { stdio: "inherit" });
const spawned = spawnProcess(command, args, options);

return wrapError(spawned);
}

/**
* Spawn a command asynchronously, streaming stdio with optional prefix.
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} [opts]
* @param {string} [prefix]
*/
// istanbul ignore next
function spawnStreaming(command, args, opts, prefix) {
const options = Object.assign({}, opts);
Expand Down Expand Up @@ -69,6 +95,10 @@ function getChildProcessCount() {
return children.size;
}

/**
* @param {import("execa").ExecaError<string>} result
* @returns {number}
*/
function getExitCode(result) {
if (result.exitCode) {
return result.exitCode;
Expand All @@ -88,6 +118,11 @@ function getExitCode(result) {
return process.exitCode;
}

/**
* @param {string} command
* @param {string[]} args
* @param {import("execa").Options} opts
*/
function spawnProcess(command, args, opts) {
const child = execa(command, args, opts);
const drain = (exitCode, signal) => {
Expand Down Expand Up @@ -116,6 +151,9 @@ function spawnProcess(command, args, opts) {
return child;
}

/**
* @param {import("execa").ExecaChildProcess<string> & { pkg?: import("@lerna/package").Package }} spawned
*/
function wrapError(spawned) {
if (spawned.pkg) {
return spawned.catch((err) => {
Expand Down

0 comments on commit 1840492

Please sign in to comment.