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

Fix repeated execPath directory #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ const npmRunPath = options => {
cwdPath = path.resolve(cwdPath, '..');
}

// Ensure the running `node` binary is used
result.push(path.dirname(process.execPath));
const execDir = getExecDir(options);

return result.concat(options.path).join(path.delimiter);
return result.concat(execDir, options.path).join(path.delimiter);
};

// Ensure the running `node` binary is used.
// Noop if the directory is already in `PATH`
const getExecDir = function (options) {
const execDir = path.normalize(path.dirname(process.execPath));
return options.path.split(path.delimiter).some(inputPath => path.normalize(inputPath) === execDir) ?
[] :
[execDir];
};

module.exports = npmRunPath;
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ test('main', t => {
path.join(__dirname, 'node_modules/.bin')
);
});

test('does not repeat execPath directory', t => {
const execDir = path.dirname(process.execPath);
const result = npmRunPath({path: execDir});
const execDirs = result.split(path.delimiter).filter(resultPath => resultPath === execDir);
t.is(execDirs.length, 1);
});

test('does not repeat execPath directory even when using a different form', t => {
const execDir = `${path.dirname(process.execPath)}/.`;
const result = npmRunPath({path: execDir});
const execDirs = result.split(path.delimiter).filter(resultPath => path.normalize(resultPath) === path.normalize(execDir));
t.is(execDirs.length, 1);
});