Sha256: b2abfc2bea4c419369f42ad4193f262e69215ff7b80222e3ffb3eaff1f586ec0

Contents?: true

Size: 1.53 KB

Versions: 33

Compression:

Stored size: 1.53 KB

Contents

const spawn = require('cross-spawn')

/** Spawns a child process, as long as you ask nicely.
 * 
 * @param {string} command - The shell command to execute.
 * @param {string[]} args - An array of arguments that are given after the command.
 * @param {string | any} [stdin] - A string that is passed to stdin.
 * @param {any} [options] - Options that are passed directly to child_process.spawn.
 * @returns {Promise<string>}
 */
const spawnPlease = (command, args, stdin, options) => {
  // if there are only three arguments and the third argument is an object, treat it as the options object and set stdin to null
  if (!options && typeof stdin === 'object') {
    options = stdin
    stdin = undefined
  }

  // defaults
  options = options || {}
  if (options.rejectOnError === undefined) {
    options.rejectOnError = true
  }

  let stdout = ''
  let stderr = ''
  const child = spawn(command, args, options)

  return new Promise((resolve, reject) => {
    if (stdin !== undefined && stdin !== null) {
      child.stdin.write(stdin)
    }
    child.stdin.end()

    child.stdout.on('data', data => {
      stdout += data
      if (options.stdout) options.stdout(data)
    })

    child.stderr.on('data', data => {
      stderr += data
      if (options.stderr) options.stderr(data)
    })

    if (options.rejectOnError) {
      child.addListener('error', reject)
    }

    child.on('close', code => {
      if (code !== 0 && options.rejectOnError) {
        reject(stderr)
      } else {
        resolve(stdout)
      }
    })
  })
}

module.exports = spawnPlease

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.59 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.58 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.57 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.56 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.55 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.54 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.53 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.52 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.51 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.50 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.49 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.48 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.47 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.46 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.45 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.44 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.43 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.42 node_modules/spawn-please/index.js
immosquare-cleaner-0.1.41 node_modules/spawn-please/index.js