index.js
This file
Path
index.js
242 lines · 5.2 KiB · JavaScript
Last changed by
[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
History of this fileTake it
1'use strict';[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 2–10why, one step back2const childProcess = require('child_process');3const util = require('util');4const crossSpawn = require('cross-spawn');5const stripEof = require('strip-eof');6const npmRunPath = require('npm-run-path');7const isStream = require('is-stream');8const _getStream = require('get-stream');9const onExit = require('signal-exit');10const errname = require('./lib/errname');fix XO lint issuesSindre Sorhusbefore adoption342096c
line 11why, one step back11[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 12why, one step back12const TEN_MEBIBYTE = 1024 * 1024 * 10;13make argument handling reusableSindre Sorhusbefore adoption9baf99e
line 14why, one step back14function handleArgs(cmd, args, opts) {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 15why, one step back15 let parsed;make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 16–20why, one step back1617 if (opts && opts.__winShell === true) {18 delete opts.__winShell;19 parsed = {20 command: cmd,[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 21why, one step back21 args,make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 22–26why, one step back22 options: opts,23 file: cmd,24 original: cmd25 };26 } else {`cross-spawn-async` → `cross-spawn` (#42)Kevin Mårtenssonbefore adoption345dc98
line 27why, one step back27 parsed = crossSpawn._parse(cmd, args, opts);make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 28–29why, one step back28 }29[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 30why, one step back30 opts = Object.assign({make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 31–32why, one step back31 maxBuffer: TEN_MEBIBYTE,32 stripEof: true,add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
line 33why, one step back33 preferLocal: true,Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76
line 34why, one step back34 encoding: 'utf8',Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 35–36why, one step back35 reject: true,36 cleanup: truemake argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 37–39why, one step back37 }, parsed.options);3839 if (opts.preferLocal) {bump `npm-run-path`Sindre Sorhusbefore adoptionbe254ee
line 40why, one step back40 opts.env = npmRunPath.env(opts);make argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 41–45why, one step back41 }4243 return {44 cmd: parsed.command,45 args: parsed.args,[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 46why, one step back46 optsmake argument handling reusableSindre Sorhusbefore adoption9baf99e
lines 47–49why, one step back47 };48}49add `input` optionJames Talmagebefore adoption605a674
line 50why, one step back50function handleInput(spawned, opts) {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 51why, one step back51 const input = opts.input;add `input` optionJames Talmagebefore adoption605a674
lines 52–63why, one step back5253 if (input === null || input === undefined) {54 return;55 }5657 if (isStream(input)) {58 input.pipe(spawned.stdin);59 } else {60 spawned.stdin.end(input);61 }62}63add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
line 64why, one step back64function handleOutput(opts, val) {Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 65why, one step back65 if (val && opts.stripEof) {add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 66–71why, one step back66 val = stripEof(val);67 }6869 return val;70}71add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
line 72why, one step back72function handleShell(fn, cmd, opts) {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 73–74why, one step back73 let file = '/bin/sh';74 let args = ['-c', cmd];add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
line 75why, one step back75[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 76why, one step back76 opts = Object.assign({}, opts);add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 77–80why, one step back7778 if (process.platform === 'win32') {79 opts.__winShell = true;80 file = process.env.comspec || 'cmd.exe';[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 81why, one step back81 args = ['/s', '/c', `"${cmd}"`];add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 82–86why, one step back82 opts.windowsVerbatimArguments = true;83 }8485 if (opts.shell) {86 file = opts.shell;fix support for the `shell` option in `execa.shell()`Sindre Sorhusbefore adoptiona80487b
line 87why, one step back87 delete opts.shell;add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 88–92why, one step back88 }8990 return fn(file, args, opts);91}92add max-buffer support (#37)James Talmagebefore adoption6288806
lines 93–94why, one step back93function getStream(process, stream, encoding, maxBuffer) {94 if (!process[stream]) {Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 95–97why, one step back95 return null;96 }97[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 98why, one step back98 let ret;add max-buffer support (#37)James Talmagebefore adoption6288806
line 99why, one step back99Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 100why, one step back100 if (encoding) {fix XO lint issuesSindre Sorhusbefore adoption342096c
line 101why, one step back101 ret = _getStream(process[stream], {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 102–103why, one step back102 encoding,103 maxBufferfix XO lint issuesSindre Sorhusbefore adoption342096c
line 104why, one step back104 });add max-buffer support (#37)James Talmagebefore adoption6288806
line 105why, one step back105 } else {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 106why, one step back106 ret = _getStream.buffer(process[stream], {maxBuffer});Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 107why, one step back107 }add max-buffer support (#37)James Talmagebefore adoption6288806
line 108why, one step back108[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 109why, one step back109 return ret.catch(err => {add max-buffer support (#37)James Talmagebefore adoption6288806
line 110why, one step back110 err.stream = stream;[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 111why, one step back111 err.message = `${stream} ${err.message}`;add max-buffer support (#37)James Talmagebefore adoption6288806
lines 112–113why, one step back112 throw err;113 });Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 114–115why, one step back114}115[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 116–119why, one step back116const processDone = spawned => new Promise(resolve => {117 spawned.on('exit', (code, signal) => {118 resolve({code, signal});119 });Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 120why, one step back120[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 121–122why, one step back121 spawned.on('error', err => {122 resolve({err});Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 123why, one step back123 });[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 124why, one step back124});Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 125why, one step back125[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 126–127why, one step back126module.exports = (cmd, args, opts) => {127 let joinedCmd = cmd;add tests (#48)James Talmagebefore adoptionb15483a
line 128why, one step back128[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 129why, one step back129 if (Array.isArray(args) && args.length > 0) {add tests (#48)James Talmagebefore adoptionb15483a
lines 130–132why, one step back130 joinedCmd += ' ' + args.join(' ');131 }132[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 133–135why, one step back133 const parsed = handleArgs(cmd, args, opts);134 const encoding = parsed.opts.encoding;135 const maxBuffer = parsed.opts.maxBuffer;ensure `execa` rejects and not throws when the core spawn throws and errorSindre Sorhusbefore adoption03964c1
line 136why, one step back136[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 137why, one step back137 let spawned;ensure `execa` rejects and not throws when the core spawn throws and errorSindre Sorhusbefore adoption03964c1
lines 138–142why, one step back138 try {139 spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts);140 } catch (err) {141 return Promise.reject(err);142 }make argument handling reusableSindre Sorhusbefore adoption9baf99e
line 143why, one step back143[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 144why, one step back144 let removeExitHandler;Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
line 145why, one step back145 if (parsed.opts.cleanup) {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 146why, one step back146 removeExitHandler = onExit(() => {Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 147–150why, one step back147 spawned.kill();148 });149 }150[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 151why, one step back151 const promise = Promise.all([Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 152why, one step back152 processDone(spawned),add max-buffer support (#37)James Talmagebefore adoption6288806
lines 153–154why, one step back153 getStream(spawned, 'stdout', encoding, maxBuffer),154 getStream(spawned, 'stderr', encoding, maxBuffer)[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 155–158why, one step back155 ]).then(arr => {156 const result = arr[0];157 const stdout = arr[1];158 const stderr = arr[2];159[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 160–162why, one step back160 let err = result.err;161 const code = result.code;162 const signal = result.signal;Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 163–166why, one step back163164 if (removeExitHandler) {165 removeExitHandler();166 }Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 167–169why, one step back167168 if (err || code !== 0 || signal !== null) {169 if (!err) {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 170why, one step back170 err = new Error(`Command failed: ${joinedCmd}\n${stderr}${stdout}`);reimplement errname lookup. (#38)James Talmagebefore adoptiona1d1764
line 171why, one step back171 err.code = code < 0 ? errname(code) : code;Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 172–173why, one step back172 }173add tests (#48)James Talmagebefore adoptionb15483a
lines 174–178why, one step back174 // TODO: missing some timeout logic for killed175 // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203176 // err.killed = spawned.killed || killed;177 err.killed = err.killed || spawned.killed;178Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 179–180why, one step back179 err.stdout = stdout;180 err.stderr = stderr;Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76
line 181why, one step back181 err.failed = true;add tests (#48)James Talmagebefore adoptionb15483a
lines 182–183why, one step back182 err.signal = signal || null;183 err.cmd = joinedCmd;Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76
lines 184–188why, one step back184185 if (!parsed.opts.reject) {186 return err;187 }188Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 189–193why, one step back189 throw err;190 }191192 return {193 stdout: handleOutput(parsed.opts, stdout),Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76
lines 194–195why, one step back194 stderr: handleOutput(parsed.opts, stderr),195 code: 0,add tests (#48)James Talmagebefore adoptionb15483a
lines 196–199why, one step back196 failed: false,197 killed: false,198 signal: null,199 cmd: joinedCmdUse spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 200why, one step back200 };201 });Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
line 202why, one step back202`cross-spawn-async` → `cross-spawn` (#42)Kevin Mårtenssonbefore adoption345dc98
line 203why, one step back203 crossSpawn._enoent.hookChildProcess(spawned, parsed);Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 204–206why, one step back204205 handleInput(spawned, parsed.opts);206return the child process instead of a promise. (#18)James Talmagebefore adoption99f10ba
lines 207–208why, one step back207 spawned.then = promise.then.bind(promise);208 spawned.catch = promise.catch.bind(promise);Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
line 209why, one step back209return the child process instead of a promise. (#18)James Talmagebefore adoption99f10ba
line 210why, one step back210 return spawned;initSindre Sorhusbefore adoptionbc16d6b
lines 211–212211};212add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505
lines 213–214why, one step back213module.exports.stdout = function () {214 // TODO: set `stderr: 'ignore'` when that option is implemented[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 215why, one step back215 return module.exports.apply(null, arguments).then(x => x.stdout);add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505
lines 216–219why, one step back216};217218module.exports.stderr = function () {219 // TODO: set `stdout: 'ignore'` when that option is implemented[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 220why, one step back220 return module.exports.apply(null, arguments).then(x => x.stderr);add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505
lines 221–222why, one step back221};222[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 223why, one step back223module.exports.shell = (cmd, opts) => handleShell(module.exports, cmd, opts);expose `.spawn`Sindre Sorhusbefore adoptionb65479c
line 224why, one step back224[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 225–226why, one step back225module.exports.sync = (cmd, args, opts) => {226 const parsed = handleArgs(cmd, args, opts);make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220
line 227why, one step back227fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 228why, one step back228 if (isStream(parsed.opts.input)) {add `input` optionJames Talmagebefore adoption605a674
lines 229–231why, one step back229 throw new TypeError('The `input` option cannot be a stream in sync mode');230 }231[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 232why, one step back232 const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
line 233why, one step back233Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 234–235why, one step back234 result.stdout = handleOutput(parsed.opts, result.stdout);235 result.stderr = handleOutput(parsed.opts, result.stderr);[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
lines 236–237why, one step back236237 return result;add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 238–239why, one step back238};239[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
lines 240–242why, one step back240module.exports.shellSync = (cmd, opts) => handleShell(module.exports.sync, cmd, opts);241242module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.');