Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it idempotent #20

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,34 @@ export const npmRunPath = ({
} = {}) => {
const cwdPath = path.resolve(toPath(cwd));
const result = [];
const pathParts = pathOption.split(path.delimiter);

if (preferLocal) {
applyPreferLocal(result, cwdPath);
applyPreferLocal(result, pathParts, cwdPath);
}

if (addExecPath) {
applyExecPath(result, execPath, cwdPath);
applyExecPath(result, pathParts, execPath, cwdPath);
}

return [...result, pathOption].join(path.delimiter);
};

const applyPreferLocal = (result, cwdPath) => {
const applyPreferLocal = (result, pathParts, cwdPath) => {
for (const directory of traversePathUp(cwdPath)) {
result.push(path.join(directory, 'node_modules/.bin'));
const pathPart = path.join(directory, 'node_modules/.bin');
if (!pathParts.includes(pathPart)) {
result.push(pathPart);
}
}
};

// Ensure the running `node` binary is used
const applyExecPath = (result, execPath, cwdPath) => {
result.push(path.resolve(cwdPath, toPath(execPath), '..'));
const applyExecPath = (result, pathParts, execPath, cwdPath) => {
const pathPart = path.resolve(cwdPath, toPath(execPath), '..');
if (!pathParts.includes(pathPart)) {
result.push(pathPart);
}
};

export const npmRunPathEnv = ({env = process.env, ...options} = {}) => {
Expand Down
24 changes: 21 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {fileURLToPath, pathToFileURL} from 'node:url';
import test from 'ava';
import {npmRunPath, npmRunPathEnv} from './index.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const localBinaryDirectory = fileURLToPath(new URL('node_modules/.bin', import.meta.url));

const testLocalDirectory = (t, addExecPath, preferLocal, expectedResult) => {
t.is(
npmRunPath({path: '', addExecPath, preferLocal}).split(path.delimiter)[0] === path.join(__dirname, 'node_modules/.bin'),
npmRunPath({path: '', addExecPath, preferLocal}).split(path.delimiter)[0] === localBinaryDirectory,
expectedResult,
);
};
Expand All @@ -20,7 +20,7 @@ test('"preferLocal: false", "addExecPath: false" does not add node_modules/.bin

const testLocalDirectoryEnv = (t, addExecPath, preferLocal, expectedResult) => {
t.is(
npmRunPathEnv({env: {PATH: 'foo'}, addExecPath, preferLocal}).PATH.split(path.delimiter)[0] === path.join(__dirname, 'node_modules/.bin'),
npmRunPathEnv({env: {PATH: 'foo'}, addExecPath, preferLocal}).PATH.split(path.delimiter)[0] === localBinaryDirectory,
expectedResult,
);
};
Expand All @@ -30,6 +30,15 @@ test('"addExecPath: false" still adds node_modules/.bin - npmRunPathEnv()', test
test('"preferLocal: false" does not add node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, undefined, false, false);
test('"preferLocal: false", "addExecPath: false" does not add node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, false, false, false);

test('node_modules/.bin is not added twice', t => {
const firstPathEnv = npmRunPath({path: ''});
const pathEnv = npmRunPath({path: firstPathEnv});
const execPaths = pathEnv
.split(path.delimiter)
.filter(pathPart => pathPart === localBinaryDirectory);
t.is(execPaths.length, 1);
});

test('the `cwd` option changes the current directory', t => {
t.is(
npmRunPath({path: '', cwd: '/dir'}).split(path.delimiter)[0],
Expand All @@ -49,6 +58,15 @@ test('push `execPath` later in the PATH', t => {
t.is(pathEnv.at(-2), path.dirname(process.execPath));
});

test('`execPath` is not added twice', t => {
const firstPathEnv = npmRunPath({path: ''});
const pathEnv = npmRunPath({path: firstPathEnv});
const execPaths = pathEnv
.split(path.delimiter)
.filter(pathPart => pathPart === path.dirname(process.execPath));
t.is(execPaths.length, 1);
});

const testExecPath = (t, preferLocal, addExecPath, expectedResult) => {
const pathEnv = npmRunPath({
path: '',
Expand Down