Skip to content

Commit cf62f37

Browse files
ehmickysindresorhus
authored andcommitted
Add execPath option (#9)
1 parent 15fe40f commit cf62f37

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ declare namespace npmRunPath {
1313
Set it to an empty string to exclude the default PATH.
1414
*/
1515
readonly path?: string;
16+
17+
/**
18+
Path to the Node.js executable to use in child processes if that is different from the current one. Its directory is pushed to the front of PATH.
19+
20+
This can be either an absolute path or a path relative to the `cwd` option.
21+
22+
@default process.execPath
23+
*/
24+
readonly execPath?: string;
1625
}
1726

1827
interface ProcessEnv {
@@ -31,6 +40,15 @@ declare namespace npmRunPath {
3140
Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options.
3241
*/
3342
readonly env?: ProcessEnv;
43+
44+
/**
45+
Path to the current Node.js executable. Its directory is pushed to the front of PATH.
46+
47+
This can be either an absolute path or a path relative to the `cwd` option.
48+
49+
@default process.execPath
50+
*/
51+
readonly execPath?: string;
3452
}
3553
}
3654

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const npmRunPath = options => {
66
options = {
77
cwd: process.cwd(),
88
path: process.env[pathKey()],
9+
execPath: process.execPath,
910
...options
1011
};
1112

@@ -20,7 +21,8 @@ const npmRunPath = options => {
2021
}
2122

2223
// Ensure the running `node` binary is used
23-
result.unshift(path.dirname(process.execPath));
24+
const execPathDir = path.resolve(options.cwd, options.execPath, '..');
25+
result.unshift(execPathDir);
2426

2527
return result.concat(options.path).join(path.delimiter);
2628
};

index.test-d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {ProcessEnv} from '.';
55
expectType<string>(npmRunPath());
66
expectType<string>(npmRunPath({cwd: '/foo'}));
77
expectType<string>(npmRunPath({path: '/usr/local/bin'}));
8+
expectType<string>(npmRunPath({execPath: '/usr/local/bin'}));
89

910
expectType<ProcessEnv>(npmRunPath.env());
1011
expectType<ProcessEnv>(npmRunPath.env({cwd: '/foo'}));
1112
expectType<ProcessEnv>(npmRunPath.env({env: process.env}));
13+
expectType<ProcessEnv>(npmRunPath.env({execPath: '/usr/local/bin'}));

readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ Default: [`PATH`](https://github.com/sindresorhus/path-key)
5656
PATH to be appended.<br>
5757
Set it to an empty string to exclude the default PATH.
5858

59+
##### execPath
60+
61+
Type: `string`<br>
62+
Default: `process.execPath`
63+
64+
Path to the current Node.js executable. Its directory is pushed to the front of PATH.
65+
66+
This can be either an absolute path or a path relative to the [`cwd` option](#cwd).
67+
5968
### npmRunPath.env(options?)
6069

6170
Returns the augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
@@ -77,6 +86,15 @@ Type: `Object`
7786

7887
Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options.
7988

89+
##### execPath
90+
91+
Type: `string`<br>
92+
Default: `process.execPath`
93+
94+
Path to the Node.js executable to use in child processes if that is different from the current one. Its directory is pushed to the front of PATH.
95+
96+
This can be either an absolute path or a path relative to the [`cwd` option](#cwd).
97+
8098

8199
## Related
82100

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ test('push `execPath` to the front of the PATH', t => {
2020
path.dirname(process.execPath)
2121
);
2222
});
23+
24+
test('can change `execPath` with the `execPath` option', t => {
25+
t.is(
26+
npmRunPath({path: '', execPath: 'test/test'}).split(path.delimiter)[0],
27+
path.resolve(process.cwd(), 'test')
28+
);
29+
});
30+
31+
test('the `execPath` option is relative to the `cwd` option', t => {
32+
t.is(
33+
npmRunPath({path: '', execPath: 'test/test', cwd: '/dir'}).split(path.delimiter)[0],
34+
path.normalize('/dir/test')
35+
);
36+
});

0 commit comments

Comments
 (0)