Skip to content

Commit d4159bf

Browse files
committed
Add execPath option
1 parent 88dbeee commit d4159bf

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

index.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ declare namespace execa {
3838
*/
3939
readonly localDir?: string;
4040

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+
@default process.execPath
49+
*/
50+
readonly execPath?: string;
51+
4152
/**
4253
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.
4354

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ const {joinCommand, parseCommand} = require('./lib/command.js');
1414

1515
const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;
1616

17-
const getEnv = ({env: envOption, extendEnv, preferLocal, localDir}) => {
17+
const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {
1818
const env = extendEnv ? {...process.env, ...envOption} : envOption;
1919

2020
if (preferLocal) {
21-
return npmRunPath.env({env, cwd: localDir});
21+
return npmRunPath.env({env, cwd: localDir, execPath});
2222
}
2323

2424
return env;
@@ -37,6 +37,7 @@ const handleArgs = (file, args, options = {}) => {
3737
extendEnv: true,
3838
preferLocal: false,
3939
localDir: options.cwd || process.cwd(),
40+
execPath: process.execPath,
4041
encoding: 'utf8',
4142
reject: true,
4243
cleanup: true,

index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ try {
7272
execa('unicorns', {cleanup: false});
7373
execa('unicorns', {preferLocal: false});
7474
execa('unicorns', {localDir: '.'});
75+
execa('unicorns', {execPath: '/path'});
7576
execa('unicorns', {buffer: false});
7677
execa('unicorns', {input: ''});
7778
execa('unicorns', {input: Buffer.from('')});

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@types/node": "^12.0.7",
5353
"ava": "^2.1.0",
5454
"coveralls": "^3.0.4",
55+
"get-node": "^5.0.0",
5556
"is-running": "^2.1.0",
5657
"nyc": "^14.1.1",
5758
"p-event": "^4.1.0",

readme.md

+11
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ Default: `process.cwd()`
308308

309309
Preferred path to find locally installed binaries in (use with `preferLocal`).
310310

311+
#### execPath
312+
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+
311322
#### buffer
312323

313324
Type: `boolean`<br>

test/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import test from 'ava';
33
import isRunning from 'is-running';
4+
import getNode from 'get-node';
45
import execa from '..';
56

67
process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
@@ -92,6 +93,12 @@ test('localDir option', async t => {
9293
t.true(envPaths.some(envPath => envPath.endsWith('.bin')));
9394
});
9495

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+
95102
test('stdin errors are handled', async t => {
96103
const child = execa('noop');
97104
child.stdin.emit('error', new Error('test'));

0 commit comments

Comments
 (0)