Add `execPath` option (#377)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent 88dbeee · Browse files · 6 files·+38 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+38 added−2 removed
- tests:+7 added−0 removed
- other:+31 added−2 removed
Provenance
- Commit
ff25ab0- Tree
5084cb8- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
88dbeee
6 files changed+38 added−2 removed
modified:test/test.js+7 added
1 import path from 'path';2 import test from 'ava';3 import isRunning from 'is-running';4+import getNode from 'get-node';5 import execa from '..';6 7 process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;8 process.env.FOO = 'foo';83 unmodified lines92 const envPaths = stdout.split(path.delimiter);93 t.true(envPaths.some(envPath => envPath.endsWith('.bin')));94 });95 96+test('execPath option', async t => {97+ const {path: execPath} = await getNode('6.0.0');98+ const {stdout} = await execa('node', ['-p', 'process.env.Path || process.env.PATH'], {preferLocal: true, execPath});99+ t.true(stdout.includes('6.0.0'));100+});101+102 test('stdin errors are handled', async t => {103 const child = execa('noop');104 child.stdin.emit('error', new Error('test'));105 await t.throwsAsync(child, /test/);120 unmodified linesmodified:index.d.ts+13 added
37 unmodified lines38 */39 readonly localDir?: string;40 41 /**42+ Path to the Node.js executable to use in child processes.43+44+ This can be either an absolute path or a path relative to the `cwd` option.45+46+ Requires `preferLocal` to be `true`.47+ 48+ For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version in a child process.49+50+ @default process.execPath51+ */52+ readonly execPath?: string;53+54+ /**55 Buffer the output from the spawned process. When set to `false`, you must read the output of `stdout` and `stderr` (or `all` if the `all` option is `true`). Otherwise the returned promise will not be resolved/rejected.56 57 If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.58 455 unmodified linesmodified:index.js+3 added-2 removed
12 unmodified lines13 const {joinCommand, parseCommand} = require('./lib/command.js');14 15 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;16 17-const getEnv = ({env: envOption, extendEnv, preferLocal, localDir}) => {17+const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {18 const env = extendEnv ? {...process.env, ...envOption} : envOption;19 20 if (preferLocal) {21- return npmRunPath.env({env, cwd: localDir});21+ return npmRunPath.env({env, cwd: localDir, execPath});22 }23 24 return env;25 };10 unmodified lines36 stripFinalNewline: true,37 extendEnv: true,38 preferLocal: false,39 localDir: options.cwd || process.cwd(),40+ execPath: process.execPath,41 encoding: 'utf8',42 reject: true,43 cleanup: true,44 all: false,212 unmodified linesmodified:index.test-d.ts+1 added
70 unmodified lines717273747576777879104 unmodified linesExpand all70 unmodified lines
execa('unicorns', {cleanup: false});execa('unicorns', {preferLocal: false});execa('unicorns', {localDir: '.'});execa('unicorns', {execPath: '/path'});execa('unicorns', {buffer: false});execa('unicorns', {input: ''});execa('unicorns', {input: Buffer.from('')});execa('unicorns', {input: process.stdin});104 unmodified linesExpand allmodified:package.json+1 added
50 unmodified lines51 "devDependencies": {52 "@types/node": "^12.0.7",53 "ava": "^2.1.0",54 "coveralls": "^3.0.4",55+ "get-node": "^5.0.0",56 "is-running": "^2.1.0",57 "nyc": "^14.1.1",58 "p-event": "^4.1.0",59 "tempfile": "^3.0.0",11 unmodified linesmodified:readme.md+13 added
306 unmodified lines307 Default: `process.cwd()`308 309 Preferred path to find locally installed binaries in (use with `preferLocal`).310 311+#### execPath312+313+Type: `string`<br>314+Default: `process.execPath` (current Node.js executable)315+316+Path to the Node.js executable to use in child processes.317+318+This can be either an absolute path or a path relative to the [`cwd` option](#cwd).319+320+Requires [`preferLocal`](#preferlocal) to be `true`.321+322+For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version in a child process.323+324 #### buffer325 326 Type: `boolean`<br>327 Default: `true`240 unmodified lines