Skip to content

Fix: fix incorrect argument parsing #4384

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

Merged
merged 10 commits into from
Sep 12, 2017
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(JSON.stringify(process.argv));
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "1.0.0",
"license": "UNLICENSED",
"scripts": {
"custom-script": "print() { echo \"A message from custom script with args \"$@\"\"; }; print "
"custom-script": "node echo.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use

"custom-script": "node -e 'console.log(`A message from custom script with args ${process.argv[process.argv.length - 1]}`)'"

To avoid the need to have a separate .js file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I did at first (I actually used -p) but Node 4 rejects additional arguments after the passed in script block. I used -- to make it ignore but this was only added in Node 7 or 6 so it failed on Node 4 builds, hence this current, "ugly" solution :(

}
}
10 changes: 4 additions & 6 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,10 @@ test.concurrent('should run help of run command if --help is before script', asy
);
});

if (process.platform !== 'win32') {
test.concurrent('should run help of custom-script if --help is after script', async () => {
const stdout = await execCommand('run', ['custom-script', '--help'], 'run-custom-script-with-arguments');
expect(stdout[stdout.length - 2]).toEqual('A message from custom script with args --help');
});
}
test.concurrent('should run help of custom-script if --help is after script', async () => {
const stdout = await execCommand('run', ['--silent', 'custom-script', '--help'], 'run-custom-script-with-arguments');
expect(JSON.parse(stdout.join('\n'))).toContain('--help');
});

test.concurrent('should run bin command', async () => {
const stdout = await execCommand('bin', [], '', true);
Expand Down
65 changes: 46 additions & 19 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,63 @@ export function main({
commander.option('--non-interactive', 'do not show interactive prompts');
commander.option('--scripts-prepend-node-path [bool]', 'prepend the node executable dir to the PATH in scripts');

// get command name
let commandName: string = args.shift() || 'install';

// if -v is the first command, then always exit after returning the version
if (commandName === '-v') {
if (args[0] === '-v') {
console.log(version.trim());
process.exitCode = 0;
return;
}

if (commandName === '--help' || commandName === '-h') {
commandName = 'help';
// get command name
const firstNonFlagIndex = args.findIndex((arg, idx, arr) => {
const isOption = arg.startsWith('-');
const prev = idx > 0 && arr[idx - 1];
const prevOption = prev && prev.startsWith('-') && commander.optionFor(prev);
const boundToPrevOption = prevOption && prevOption.required;

return !isOption && !boundToPrevOption;
});
let preCommandArgs;
let commandName = '';
if (firstNonFlagIndex > -1) {
preCommandArgs = args.slice(0, firstNonFlagIndex);
commandName = args[firstNonFlagIndex];
args = args.slice(firstNonFlagIndex + 1);
} else {
preCommandArgs = args;
args = [];
}

if (Object.prototype.hasOwnProperty.call(commands, commandName) && (args[0] === '--help' || args[0] === '-h')) {
args.unshift(commandName);
let isKnownCommand = Object.prototype.hasOwnProperty.call(commands, commandName);
const isHelp = arg => arg === '--help' || arg === '-h';
const helpInPre = preCommandArgs.findIndex(isHelp);
const helpInArgs = args.findIndex(isHelp);
const setHelpMode = () => {
if (isKnownCommand) {
args.unshift(commandName);
}
commandName = 'help';
}
isKnownCommand = true;
};

// if no args or command name looks like a flag then set default to `install`
if (commandName[0] === '-') {
args.unshift(commandName);
commandName = 'install';
if (helpInPre > -1) {
preCommandArgs.splice(helpInPre);
setHelpMode();
} else if (isKnownCommand && helpInArgs === 0) {
args.splice(helpInArgs);
setHelpMode();
}

let command;
if (Object.prototype.hasOwnProperty.call(commands, commandName)) {
command = commands[commandName];
if (!commandName) {
commandName = 'install';
isKnownCommand = true;
}

// if command is not recognized, then set default to `run`
if (!command) {
if (isKnownCommand) {
command = commands[commandName];
} else {
// if command is not recognized, then set default to `run`
args.unshift(commandName);
command = commands.run;
}
Expand All @@ -139,6 +164,8 @@ export function main({
}
}

args = [...preCommandArgs, ...args];

command.setFlags(commander);
commander.parse([
...startArgs,
Expand Down Expand Up @@ -489,8 +516,8 @@ export function main({
onUnexpectedError(err);
}

if (commands[commandName]) {
reporter.info(commands[commandName].getDocsInfo);
if (command.getDocsInfo) {
reporter.info(command.getDocsInfo);
}

return exit(1);
Expand Down