Skip to content
Myr

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 file

Take it

initSindre Sorhusbefore adoptionbc16d6b

line 1
1'use strict';

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 2–10why, one step back
2const 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 back
11

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 12why, one step back
12const TEN_MEBIBYTE = 1024 * 1024 * 10;

initSindre Sorhusbefore adoptionbc16d6b

line 13
13

make argument handling reusableSindre Sorhusbefore adoption9baf99e

line 14why, one step back
14function handleArgs(cmd, args, opts) {

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 15why, one step back
15 let parsed;

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 16–20why, one step back
16
17 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 back
21 args,

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 22–26why, one step back
22 options: opts,
23 file: cmd,24 original: cmd25 };26 } else {

`cross-spawn-async` → `cross-spawn` (#42)Kevin Mårtenssonbefore adoption345dc98

line 27why, one step back
27 parsed = crossSpawn._parse(cmd, args, opts);

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 28–29why, one step back
28 }
29

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 30why, one step back
30 opts = Object.assign({

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 31–32why, one step back
31 maxBuffer: TEN_MEBIBYTE,
32 stripEof: true,

add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c

line 33why, one step back
33 preferLocal: true,

Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76

line 34why, one step back
34 encoding: 'utf8',

Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd

lines 35–36why, one step back
35 reject: true,
36 cleanup: true

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 37–39why, one step back
37 }, parsed.options);
3839 if (opts.preferLocal) {

bump `npm-run-path`Sindre Sorhusbefore adoptionbe254ee

line 40why, one step back
40 opts.env = npmRunPath.env(opts);

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 41–45why, one step back
41 }
4243 return {44 cmd: parsed.command,45 args: parsed.args,

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 46why, one step back
46 opts

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 47–49why, one step back
47 };
48}49

add `input` optionJames Talmagebefore adoption605a674

line 50why, one step back
50function handleInput(spawned, opts) {

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 51why, one step back
51 const input = opts.input;

add `input` optionJames Talmagebefore adoption605a674

lines 52–63why, one step back
52
53 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}63

add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c

line 64why, one step back
64function handleOutput(opts, val) {

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

line 65why, one step back
65 if (val && opts.stripEof) {

add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c

lines 66–71why, one step back
66 val = stripEof(val);
67 }6869 return val;70}71

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

line 72why, one step back
72function handleShell(fn, cmd, opts) {

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 73–74why, one step back
73 let file = '/bin/sh';
74 let args = ['-c', cmd];

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

line 75why, one step back
75

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 76why, one step back
76 opts = Object.assign({}, opts);

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

lines 77–80why, one step back
77
78 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 back
81 args = ['/s', '/c', `"${cmd}"`];

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

lines 82–86why, one step back
82 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 back
87 delete opts.shell;

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

lines 88–92why, one step back
88 }
8990 return fn(file, args, opts);91}92

add max-buffer support (#37)James Talmagebefore adoption6288806

lines 93–94why, one step back
93function getStream(process, stream, encoding, maxBuffer) {
94 if (!process[stream]) {

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 95–97why, one step back
95 return null;
96 }97

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 98why, one step back
98 let ret;

add max-buffer support (#37)James Talmagebefore adoption6288806

line 99why, one step back
99

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

line 100why, one step back
100 if (encoding) {

fix XO lint issuesSindre Sorhusbefore adoption342096c

line 101why, one step back
101 ret = _getStream(process[stream], {

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 102–103why, one step back
102 encoding,
103 maxBuffer

fix XO lint issuesSindre Sorhusbefore adoption342096c

line 104why, one step back
104 });

add max-buffer support (#37)James Talmagebefore adoption6288806

line 105why, one step back
105 } else {

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 106why, one step back
106 ret = _getStream.buffer(process[stream], {maxBuffer});

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

line 107why, one step back
107 }

add max-buffer support (#37)James Talmagebefore adoption6288806

line 108why, one step back
108

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 109why, one step back
109 return ret.catch(err => {

add max-buffer support (#37)James Talmagebefore adoption6288806

line 110why, one step back
110 err.stream = stream;

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 111why, one step back
111 err.message = `${stream} ${err.message}`;

add max-buffer support (#37)James Talmagebefore adoption6288806

lines 112–113why, one step back
112 throw err;
113 });

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 114–115why, one step back
114}
115

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 116–119why, one step back
116const 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 back
120

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 121–122why, one step back
121 spawned.on('error', err => {
122 resolve({err});

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

line 123why, one step back
123 });

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 124why, one step back
124});

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

line 125why, one step back
125

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 126–127why, one step back
126module.exports = (cmd, args, opts) => {
127 let joinedCmd = cmd;

add tests (#48)James Talmagebefore adoptionb15483a

line 128why, one step back
128

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 129why, one step back
129 if (Array.isArray(args) && args.length > 0) {

add tests (#48)James Talmagebefore adoptionb15483a

lines 130–132why, one step back
130 joinedCmd += ' ' + args.join(' ');
131 }132

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 133–135why, one step back
133 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 back
136

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 137why, one step back
137 let spawned;

ensure `execa` rejects and not throws when the core spawn throws and errorSindre Sorhusbefore adoption03964c1

lines 138–142why, one step back
138 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 back
143

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 144why, one step back
144 let removeExitHandler;

Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd

line 145why, one step back
145 if (parsed.opts.cleanup) {

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 146why, one step back
146 removeExitHandler = onExit(() => {

Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd

lines 147–150why, one step back
147 spawned.kill();
148 });149 }150

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 151why, one step back
151 const promise = Promise.all([

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

line 152why, one step back
152 processDone(spawned),

add max-buffer support (#37)James Talmagebefore adoption6288806

lines 153–154why, one step back
153 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 back
155 ]).then(arr => {
156 const result = arr[0];157 const stdout = arr[1];158 const stderr = arr[2];

initSindre Sorhusbefore adoptionbc16d6b

line 159
159

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 160–162why, one step back
160 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 back
163
164 if (removeExitHandler) {165 removeExitHandler();166 }

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 167–169why, one step back
167
168 if (err || code !== 0 || signal !== null) {169 if (!err) {

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 170why, one step back
170 err = new Error(`Command failed: ${joinedCmd}\n${stderr}${stdout}`);

reimplement errname lookup. (#38)James Talmagebefore adoptiona1d1764

line 171why, one step back
171 err.code = code < 0 ? errname(code) : code;

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 172–173why, one step back
172 }
173

add tests (#48)James Talmagebefore adoptionb15483a

lines 174–178why, one step back
174 // TODO: missing some timeout logic for killed
175 // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203176 // err.killed = spawned.killed || killed;177 err.killed = err.killed || spawned.killed;178

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 179–180why, one step back
179 err.stdout = stdout;
180 err.stderr = stderr;

Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76

line 181why, one step back
181 err.failed = true;

add tests (#48)James Talmagebefore adoptionb15483a

lines 182–183why, one step back
182 err.signal = signal || null;
183 err.cmd = joinedCmd;

Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76

lines 184–188why, one step back
184
185 if (!parsed.opts.reject) {186 return err;187 }188

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 189–193why, one step back
189 throw err;
190 }191192 return {193 stdout: handleOutput(parsed.opts, stdout),

Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76

lines 194–195why, one step back
194 stderr: handleOutput(parsed.opts, stderr),
195 code: 0,

add tests (#48)James Talmagebefore adoptionb15483a

lines 196–199why, one step back
196 failed: false,
197 killed: false,198 signal: null,199 cmd: joinedCmd

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

line 200why, one step back
200 };

initSindre Sorhusbefore adoptionbc16d6b

line 201
201 });

Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484

line 202why, one step back
202

`cross-spawn-async` → `cross-spawn` (#42)Kevin Mårtenssonbefore adoption345dc98

line 203why, one step back
203 crossSpawn._enoent.hookChildProcess(spawned, parsed);

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 204–206why, one step back
204
205 handleInput(spawned, parsed.opts);206

return the child process instead of a promise. (#18)James Talmagebefore adoption99f10ba

lines 207–208why, one step back
207 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 back
209

return the child process instead of a promise. (#18)James Talmagebefore adoption99f10ba

line 210why, one step back
210 return spawned;

initSindre Sorhusbefore adoptionbc16d6b

lines 211–212
211};
212

add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505

lines 213–214why, one step back
213module.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 back
215 return module.exports.apply(null, arguments).then(x => x.stdout);

add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505

lines 216–219why, one step back
216};
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 back
220 return module.exports.apply(null, arguments).then(x => x.stderr);

add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505

lines 221–222why, one step back
221};
222

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

line 223why, one step back
223module.exports.shell = (cmd, opts) => handleShell(module.exports, cmd, opts);

expose `.spawn`Sindre Sorhusbefore adoptionb65479c

line 224why, one step back
224

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 225–226why, one step back
225module.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 back
227

fix option handling in #15Sindre Sorhusbefore adoption3d31e8d

line 228why, one step back
228 if (isStream(parsed.opts.input)) {

add `input` optionJames Talmagebefore adoption605a674

lines 229–231why, one step back
229 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 back
232 const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);

add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c

line 233why, one step back
233

Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87

lines 234–235why, one step back
234 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 back
236
237 return result;

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

lines 238–239why, one step back
238};
239

[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c

lines 240–242why, one step back
240module.exports.shellSync = (cmd, opts) => handleShell(module.exports.sync, cmd, opts);
241242module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.');