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: cmd-shim not identified if placed outside node_modules/.bin/ #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const resolveCommand = require('./util/resolveCommand');
const escape = require('./util/escape');
const readShebang = require('./util/readShebang');
const readCmdShim = require('read-cmd-shim');

const isWin = process.platform === 'win32';
const isExecutableRegExp = /\.(?:com|exe)$/i;
Expand All @@ -24,6 +25,23 @@ function detectShebang(parsed) {
return parsed.file;
}

function isCmdShim(commandFile) {
// Check if file is located in `node_modules/.bin/`
if (isCmdShimRegExp.test(commandFile)) {
return true;
}
// A cmd-shim does not necessarily have to be in `node_modules/.bin/`,
// such as one installed globally through 'npm i -g'. So we test by reading
// its contents and see if we can obtain the wrapped command.
try {
const dest = readCmdShim.sync(commandFile.toLowerCase());

return !!dest;
} catch (error) {
return false;
}
}

function parseNonShell(parsed) {
if (!isWin) {
return parsed;
Expand All @@ -38,11 +56,11 @@ function parseNonShell(parsed) {
// If a shell is required, use cmd.exe and take care of escaping everything correctly
// Note that `forceShell` is an hidden option used only in tests
if (parsed.options.forceShell || needsShell) {
// Need to double escape meta chars if the command is a cmd-shim located in `node_modules/.bin/`
// Need to double escape meta chars if the command is a cmd-shim.
// The cmd-shim simply calls execute the package bin file with NodeJS, proxying any argument
// Because the escape of metachars with ^ gets interpreted when the cmd.exe is first called,
// we need to double escape them
const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
const needsDoubleEscapeMetaChars = isCmdShim(commandFile);

// Normalize posix paths into OS compatible paths (e.g.: foo/bar -> foo\bar)
// This is necessary otherwise it will always fail with ENOENT in those cases
Expand Down
Loading