add option to prefer locally installed binaries
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 0ea8aaf · Browse files · 4 files·+44 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+44 added−10 removed
- other:+44 added−10 removed
Provenance
- Commit
5bf5f99- Tree
167e23c- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
0ea8aaf
4 files changed+44 added−10 removed
modified:index.js+18 added-6 removed
1234567891011101112131213141516171819201721222324252627282930313233343524363738394034 unmodified lines'use strict';var childProcess = require('child_process');var crossSpawnAsync = require('cross-spawn-async');var stripEof = require('strip-eof');var objectAssign = require('object-assign');var npmRunPath = require('npm-run-path');var pathKey = require('path-key')();var TEN_MEBIBYTE = 1024 * 1024 * 10;
module.exports = function (cmd, args, opts) { return new Promise(function (resolve, reject) { opts = objectAssign({ maxBuffer: TEN_MEBIBYTE }, opts);
var parsed = crossSpawnAsync._parse(cmd, args, opts);
opts = objectAssign({ maxBuffer: TEN_MEBIBYTE, stripEof: true, preferLocal: true }, parsed.options);
var handle = function (val) { if (parsed.options.stripEof !== false) { if (opts.stripEof) { val = stripEof(val); }
return val; };
if (opts.preferLocal) { opts.env = opts.env || {}; opts.env[pathKey] = npmRunPath({ cwd: parsed.cwd, path: opts.env[pathKey] }); }
var spawned = childProcess.execFile(parsed.command, parsed.args, parsed.options, function (err, stdout, stderr) { var spawned = childProcess.execFile(parsed.command, parsed.args, opts, function (err, stdout, stderr) { if (err) { err.stdout = stdout; err.stderr = stderr; reject(err);34 unmodified linesmodified:package.json+3 added
29 unmodified lines30313233343536373839404142434429 unmodified lines "binary" ], "dependencies": { "cross-spawn-async": "^2.1.1", "npm-run-path": "^1.0.0", "object-assign": "^4.0.1", "path-key": "^1.0.0", "strip-eof": "^1.0.0" }, "devDependencies": { "ava": "*", "cat-names": "^1.0.2", "xo": "*" }}modified:readme.md+10 added-1 removed
8 unmodified lines9 - [Strips EOF](https://github.com/sindresorhus/strip-eof) from the output so you don't have to `stdout.trim()`.10 - Supports [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) binaries cross-platform.11 - [Improved Windows support.](https://github.com/IndigoUnited/node-cross-spawn-async#why)12 - Higher max buffer. 10 MB instead of 200 KB.13+- [Executes locally installed binaries by name.](#preferlocal)14 15 16 ## Install17 58 unmodified lines76 Additional exposed options:77 78 #### stripEof79 79-Type: `boolean` 80+Type: `boolean`<br>81 Default: `true`82 83 [Strip EOF](https://github.com/sindresorhus/strip-eof) (last newline) from the output.84 85+#### preferLocal86 87+Type: `boolean`<br>88+Default: `true`89+90+Prefer locally installed binaries when looking for a binary to execute.<br>91+If you `$ npm install foo`, you can then `execa('foo')`.92+93+94 ## License95 96 MIT © [Sindre Sorhus](http://sindresorhus.com)modified:test.js+13 added-3 removed
19 unmodified lines202122232425262728293031323329303134353637383940414219 unmodified lines t.is(typeof err.stdout, 'string'); }});
test('execa.shell()', async t => { const {stdout} = await fn.shell('echo foo'); t.is(stdout, 'foo');});
test('stripEof option', async t => { const {stdout} = await fn('echo', ['foo'], {stripEof: false}); t.is(stdout, 'foo\n');});
test('execa.shell()', async t => { const {stdout} = await fn.shell('echo foo'); t.is(stdout, 'foo');test.serial('preferLocal option', async t => { t.true((await fn('cat-names')).stdout.length > 2);
// account for npm adding local binaries to the PATH const _path = process.env.PATH; process.env.PATH = ''; await t.throws(fn('cat-names', {preferLocal: false}), /spawn cat-names ENOENT/); process.env.PATH = _path;});