@netlify/run-utils
Version:
Utility for running commands inside Netlify Build
41 lines (40 loc) • 1.51 kB
JavaScript
import process from 'process';
import { execa, execaCommand } from 'execa';
/** Allow running local binaries by default */
const DEFAULT_OPTIONS = { preferLocal: true };
/** Run a command, with arguments being an array */
export const run = (file, args, options) => {
const [argsA, optionsA] = parseArgs(args, options);
const optionsB = { ...DEFAULT_OPTIONS, ...optionsA };
const childProcess = execa(file, argsA, optionsB);
redirectOutput(childProcess, optionsB);
return childProcess;
};
/** Run a command, with file + arguments being a single string */
export const runCommand = (command, options) => {
const optionsA = { ...DEFAULT_OPTIONS, ...options };
const childProcess = execaCommand(command, optionsA);
redirectOutput(childProcess, optionsA);
return childProcess;
};
/** Both `args` and `options` are optional */
const parseArgs = function (args, options) {
if (Array.isArray(args)) {
return [args, options];
}
if (typeof args === 'object') {
return [[], args];
}
return [];
};
/**
* Redirect output by default, unless specified otherwise
* */
const redirectOutput = (childProcess, options) => {
var _a, _b;
if (options.stdio !== undefined || options.stdout !== undefined || options.stderr !== undefined) {
return;
}
(_a = childProcess.stdout) === null || _a === void 0 ? void 0 : _a.pipe(process.stdout);
(_b = childProcess.stderr) === null || _b === void 0 ? void 0 : _b.pipe(process.stderr);
};