index.js
This file
Path
index.js
152 lines · 3.2 KiB · JavaScript
Last changed by
add `input` optionJames Talmagebefore adoption605a674
History of this fileTake it
1'use strict';2var childProcess = require('child_process');use the `cross-spawn-async` API instead of the files directlySindre Sorhusbefore adoptionf4a645d
line 3why, one step back3var crossSpawnAsync = require('cross-spawn-async');4var stripEof = require('strip-eof');5var objectAssign = require('object-assign');Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 6why, one step back6var npmRunPath = require('npm-run-path');add `input` optionJames Talmagebefore adoption605a674
line 7why, one step back7var isStream = require('is-stream');fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 8why, one step back8var pathKey = require('path-key')();initSindre Sorhusbefore adoptionbc16d6b
lines 9–109var TEN_MEBIBYTE = 1024 * 1024 * 10;10make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 11–29why, one step back11function handleArgs(cmd, args, opts) {12 var parsed;1314 if (opts && opts.__winShell === true) {15 delete opts.__winShell;16 parsed = {17 command: cmd,18 args: args,19 options: opts,20 file: cmd,21 original: cmd22 };23 } else {24 parsed = crossSpawnAsync._parse(cmd, args, opts);25 }2627 opts = objectAssign({28 maxBuffer: TEN_MEBIBYTE,29 stripEof: true,add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 30–31why, one step back30 preferLocal: true,31 encoding: 'utf8'make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 32–35why, one step back32 }, parsed.options);3334 if (opts.preferLocal) {35 opts.env = objectAssign({}, opts.env || process.env);fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 36why, one step back36 opts.env[pathKey] = npmRunPath({make argument handling reusableSindre Sorhusbefore adoption9baf99e
line 37why, one step back37 cwd: opts.cwd,fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 38why, one step back38 path: opts.env[pathKey]make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 39–48why, one step back39 });40 }4142 return {43 cmd: parsed.command,44 args: parsed.args,45 opts: opts46 };47}48add `input` optionJames Talmagebefore adoption605a674
lines 49–62why, one step back49function handleInput(spawned, opts) {50 var input = opts.input;5152 if (input === null || input === undefined) {53 return;54 }5556 if (isStream(input)) {57 input.pipe(spawned.stdin);58 } else {59 spawned.stdin.end(input);60 }61}62add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 63–70why, one step back63function handleOutput(opts, val) {64 if (opts.stripEof) {65 val = stripEof(val);66 }6768 return val;69}70add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 71–93why, one step back71function handleShell(fn, cmd, opts) {72 var file;73 var args;7475 opts = objectAssign({}, opts);7677 if (process.platform === 'win32') {78 opts.__winShell = true;79 file = process.env.comspec || 'cmd.exe';80 args = ['/s', '/c', '"' + cmd + '"'];81 opts.windowsVerbatimArguments = true;82 } else {83 file = '/bin/sh';84 args = ['-c', cmd];85 }8687 if (opts.shell) {88 file = opts.shell;89 }9091 return fn(file, args, opts);92}9394module.exports = function (cmd, args, opts) {Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
line 95why, one step back95 var spawned;make argument handling reusableSindre Sorhusbefore adoption9baf99e
line 96why, one step back96Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
line 97why, one step back97 var promise = new Promise(function (resolve, reject) {make argument handling reusableSindre Sorhusbefore adoption9baf99e
line 98why, one step back98 var parsed = handleArgs(cmd, args, opts);99make argument handling reusableSindre Sorhusbefore adoption9baf99e
line 100why, one step back100 spawned = childProcess.execFile(parsed.cmd, parsed.args, parsed.opts, function (err, stdout, stderr) {101 if (err) {add `stdout`/`stderr` to errorsSindre Sorhusbefore adoption0ea8aaf
lines 102–103why, one step back102 err.stdout = stdout;103 err.stderr = stderr;include stdout in errors for improved debuggingSindre Sorhusbefore adoptionaea8c9a
line 104why, one step back104 err.message += stdout;initSindre Sorhusbefore adoptionbc16d6b
lines 105–109105 reject(err);106 return;107 }108109 resolve({add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 110–111why, one step back110 stdout: handleOutput(parsed.opts, stdout),111 stderr: handleOutput(parsed.opts, stderr)initSindre Sorhusbefore adoptionbc16d6b
lines 112–114112 });113 });114use the `cross-spawn-async` API instead of the files directlySindre Sorhusbefore adoptionf4a645d
line 115why, one step back115 crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);add `input` optionJames Talmagebefore adoption605a674
lines 116–117why, one step back116117 handleInput(spawned, parsed.opts);118 });Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
lines 119–123why, one step back119120 promise.kill = spawned.kill.bind(spawned);121 promise.pid = spawned.pid;122123 return promise;initSindre Sorhusbefore adoptionbc16d6b
lines 124–126124};125126module.exports.shell = function (cmd, opts) {add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
line 127why, one step back127 return handleShell(module.exports, cmd, opts);128};expose `.spawn`Sindre Sorhusbefore adoptionb65479c
line 129why, one step back129make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220
lines 130–136why, one step back130module.exports.spawn = function (cmd, args, opts) {131 var parsed = handleArgs(cmd, args, opts);132 var spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts);133134 crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);135136 return spawned;add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 137–139why, one step back137};138139module.exports.sync = function (cmd, args, opts) {add `input` optionJames Talmagebefore adoption605a674
lines 140–143why, one step back140 if (opts && isStream(opts.input)) {141 throw new TypeError('The `input` option cannot be a stream in sync mode');142 }143add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 144–147why, one step back144 var parsed = handleArgs(cmd, args, opts);145 var out = childProcess.execFileSync(parsed.cmd, parsed.args, parsed.opts);146147 return handleOutput(parsed.opts, out);add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 148–151why, one step back148};149150module.exports.shellSync = function (cmd, opts) {151 return handleShell(module.exports.sync, cmd, opts);make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220
line 152why, one step back152};