UNPKG

@jsdevtools/ez-spawn

Version:

Simple, consistent sync or async process spawning

43 lines (36 loc) 1.14 kB
"use strict"; const normalizeArgs = require("./normalize-args"); const normalizeResult = require("./normalize-result"); const spawnSync = require("cross-spawn").sync; module.exports = sync; /** * Executes the given command synchronously, and returns the buffered results. * * @param {string|string[]} command - The command to run * @param {string|string[]} [args] - The command arguments * @param {object} [options] - options * @returns {Process} * * @see {@link normalizeArgs} for argument details */ function sync () { // Normalize the function arguments let { command, args, options, error } = normalizeArgs(arguments); if (error) { // Invalid arguments normalizeResult({ command, args, options, error }); } else { let result; try { // Run the program result = spawnSync(command, args, options); } catch (error) { // An error occurred while spawning or killing the process normalizeResult({ error, command, args, options }); } // Return the results or throw an error return normalizeResult(Object.assign({}, result, { command, args, options })); } }