Skip to content

Commit

Permalink
Allow execPath to be a file URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 21, 2023
1 parent 99c4be7 commit 17114cb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface RunPathOptions {
@default process.execPath
*/
readonly execPath?: string;
readonly execPath?: string | URL;
}

export type ProcessEnv = Record<string, string | undefined>;
Expand All @@ -45,7 +45,7 @@ export interface EnvOptions {
@default process.execPath
*/
readonly execPath?: string;
readonly execPath?: string | URL;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function npmRunPath(options = {}) {
} = options;

let previous;
const execPathString = execPath instanceof URL ? url.fileURLToPath(execPath) : execPath;
const cwdString = cwd instanceof URL ? url.fileURLToPath(cwd) : cwd;
let cwdPath = path.resolve(cwdString);
const result = [];
Expand All @@ -22,7 +23,7 @@ export function npmRunPath(options = {}) {
}

// Ensure the running `node` binary is used.
result.push(path.resolve(cwdString, execPath, '..'));
result.push(path.resolve(cwdString, execPathString, '..'));

return [...result, path_].join(path.delimiter);
}
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ expectType<string>(npmRunPath({cwd: '/foo'}));
expectType<string>(npmRunPath({cwd: new URL('file:///foo')}));
expectType<string>(npmRunPath({path: '/usr/local/bin'}));
expectType<string>(npmRunPath({execPath: '/usr/local/bin'}));
expectType<string>(npmRunPath({execPath: new URL('file:///usr/local/bin')}));

expectType<ProcessEnv>(npmRunPathEnv());
expectType<ProcessEnv>(npmRunPathEnv({cwd: '/foo'}));
expectType<ProcessEnv>(npmRunPathEnv({cwd: new URL('file:///foo')}));
expectType<ProcessEnv>(npmRunPathEnv({env: process.env})); // eslint-disable-line @typescript-eslint/no-unsafe-assignment
expectType<ProcessEnv>(npmRunPathEnv({execPath: '/usr/local/bin'}));
expectType<ProcessEnv>(npmRunPathEnv({execPath: new URL('file:///usr/local/bin')}));
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Set it to an empty string to exclude the default PATH.

##### execPath

Type: `string`\
Type: `string | URL`\
Default: `process.execPath`

The path to the current Node.js executable. Its directory is pushed to the front of PATH.
Expand Down
10 changes: 9 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {fileURLToPath, pathToFileURL} from 'node:url';
import test from 'ava';
import {npmRunPath, npmRunPathEnv} from './index.js';

Expand Down Expand Up @@ -44,6 +44,13 @@ test('can change `execPath` with the `execPath` option', t => {
t.is(pathEnv[pathEnv.length - 2], path.resolve(process.cwd(), 'test'));
});

test('the `execPath` option can be a file URL', t => {
const pathEnv = npmRunPath({path: '', execPath: pathToFileURL('test/test')}).split(
path.delimiter,
);
t.is(pathEnv[pathEnv.length - 2], path.resolve(process.cwd(), 'test'));
});

test('the `execPath` option is relative to the `cwd` option', t => {
const pathEnv = npmRunPath({
path: '',
Expand All @@ -52,3 +59,4 @@ test('the `execPath` option is relative to the `cwd` option', t => {
}).split(path.delimiter);
t.is(pathEnv[pathEnv.length - 2], path.normalize('/dir/test'));
});

0 comments on commit 17114cb

Please sign in to comment.