Skip to content
Myr

index.js

This file

Path

index.js

152 lines · 3.2 KiB · JavaScript

Last changed by

add `input` optionJames Talmagebefore adoption605a674

History of this file

Take it

initSindre Sorhusbefore adoptionbc16d6b

lines 1–2
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 back
3var crossSpawnAsync = require('cross-spawn-async');

initSindre Sorhusbefore adoptionbc16d6b

lines 4–5
4var stripEof = require('strip-eof');
5var objectAssign = require('object-assign');

Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c

line 6why, one step back
6var npmRunPath = require('npm-run-path');

add `input` optionJames Talmagebefore adoption605a674

line 7why, one step back
7var isStream = require('is-stream');

fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df

line 8why, one step back
8var pathKey = require('path-key')();

initSindre Sorhusbefore adoptionbc16d6b

lines 9–10
9var TEN_MEBIBYTE = 1024 * 1024 * 10;
10

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 11–29why, one step back
11function 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 back
30 preferLocal: true,
31 encoding: 'utf8'

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 32–35why, one step back
32 }, 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 back
36 opts.env[pathKey] = npmRunPath({

make argument handling reusableSindre Sorhusbefore adoption9baf99e

line 37why, one step back
37 cwd: opts.cwd,

fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df

line 38why, one step back
38 path: opts.env[pathKey]

make argument handling reusableSindre Sorhusbefore adoption9baf99e

lines 39–48why, one step back
39 });
40 }4142 return {43 cmd: parsed.command,44 args: parsed.args,45 opts: opts46 };47}48

add `input` optionJames Talmagebefore adoption605a674

lines 49–62why, one step back
49function 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}62

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

lines 63–70why, one step back
63function handleOutput(opts, val) {
64 if (opts.stripEof) {65 val = stripEof(val);66 }6768 return val;69}70

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

lines 71–93why, one step back
71function 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}93

initSindre Sorhusbefore adoptionbc16d6b

line 94
94module.exports = function (cmd, args, opts) {

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

line 95why, one step back
95 var spawned;

make argument handling reusableSindre Sorhusbefore adoption9baf99e

line 96why, one step back
96

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

line 97why, one step back
97 var promise = new Promise(function (resolve, reject) {

make argument handling reusableSindre Sorhusbefore adoption9baf99e

line 98why, one step back
98 var parsed = handleArgs(cmd, args, opts);

initSindre Sorhusbefore adoptionbc16d6b

line 99
99

make argument handling reusableSindre Sorhusbefore adoption9baf99e

line 100why, one step back
100 spawned = childProcess.execFile(parsed.cmd, parsed.args, parsed.opts, function (err, stdout, stderr) {

initSindre Sorhusbefore adoptionbc16d6b

line 101
101 if (err) {

add `stdout`/`stderr` to errorsSindre Sorhusbefore adoption0ea8aaf

lines 102–103why, one step back
102 err.stdout = stdout;
103 err.stderr = stderr;

include stdout in errors for improved debuggingSindre Sorhusbefore adoptionaea8c9a

line 104why, one step back
104 err.message += stdout;

initSindre Sorhusbefore adoptionbc16d6b

lines 105–109
105 reject(err);
106 return;107 }108109 resolve({

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

lines 110–111why, one step back
110 stdout: handleOutput(parsed.opts, stdout),
111 stderr: handleOutput(parsed.opts, stderr)

initSindre Sorhusbefore adoptionbc16d6b

lines 112–114
112 });
113 });114

use the `cross-spawn-async` API instead of the files directlySindre Sorhusbefore adoptionf4a645d

line 115why, one step back
115 crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);

add `input` optionJames Talmagebefore adoption605a674

lines 116–117why, one step back
116
117 handleInput(spawned, parsed.opts);

initSindre Sorhusbefore adoptionbc16d6b

line 118
118 });

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

lines 119–123why, one step back
119
120 promise.kill = spawned.kill.bind(spawned);121 promise.pid = spawned.pid;122123 return promise;

initSindre Sorhusbefore adoptionbc16d6b

lines 124–126
124};
125126module.exports.shell = function (cmd, opts) {

add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee

line 127why, one step back
127 return handleShell(module.exports, cmd, opts);

initSindre Sorhusbefore adoptionbc16d6b

line 128
128};

expose `.spawn`Sindre Sorhusbefore adoptionb65479c

line 129why, one step back
129

make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220

lines 130–136why, one step back
130module.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 back
137};
138139module.exports.sync = function (cmd, args, opts) {

add `input` optionJames Talmagebefore adoption605a674

lines 140–143why, one step back
140 if (opts && isStream(opts.input)) {
141 throw new TypeError('The `input` option cannot be a stream in sync mode');142 }143

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

lines 144–147why, one step back
144 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 back
148};
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 back
152};