Skip to content

Commit 24c590a

Browse files
hanslalexeagle
authored andcommitted
fix(@angular/cli): if no command, properly show help
Using the first flag is bad, but we still support --version and --help.
1 parent aa47ce7 commit 24c590a

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

packages/angular/cli/models/command-runner.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export async function runCommand(commandMap: CommandMap,
6262
args = ['help'];
6363
}
6464
const rawOptions = yargsParser(args, { alias: { help: ['h'] }, boolean: [ 'help' ] });
65-
let commandName = rawOptions._[0];
65+
let commandName = rawOptions._[0] || '';
6666

6767
// remove the command name
6868
rawOptions._ = rawOptions._.slice(1);
@@ -71,39 +71,48 @@ export async function runCommand(commandMap: CommandMap,
7171
: CommandScope.outsideProject;
7272

7373
let Cmd: CommandConstructor | null;
74-
Cmd = findCommand(commandMap, commandName);
74+
Cmd = commandName ? findCommand(commandMap, commandName) : null;
7575

76-
if (!Cmd && !commandName && (rawOptions.v || rawOptions.version)) {
76+
if (!Cmd && (rawOptions.v || rawOptions.version)) {
7777
commandName = 'version';
7878
Cmd = findCommand(commandMap, commandName);
79-
}
80-
81-
if (!Cmd && rawOptions.help) {
79+
} else if (!Cmd && (!commandName || rawOptions.help)) {
8280
commandName = 'help';
8381
Cmd = findCommand(commandMap, commandName);
8482
}
8583

8684
if (!Cmd) {
87-
const commandsDistance = {} as { [name: string]: number };
88-
const allCommands = listAllCommandNames(commandMap).sort((a, b) => {
89-
if (!(a in commandsDistance)) {
90-
commandsDistance[a] = levenshtein(a, commandName);
91-
}
92-
if (!(b in commandsDistance)) {
93-
commandsDistance[b] = levenshtein(b, commandName);
94-
}
85+
if (!commandName) {
86+
logger.error(tags.stripIndent`
87+
We could not find a command from the arguments and the help command seems to be disabled.
88+
This is an issue with the CLI itself. If you see this comment, please report it and
89+
provide your repository.
90+
`);
9591

96-
return commandsDistance[a] - commandsDistance[b];
97-
});
92+
return 1;
93+
} else {
94+
// Set name to string (no undefined).
95+
const commandsDistance = {} as { [name: string]: number };
96+
const allCommands = listAllCommandNames(commandMap).sort((a, b) => {
97+
if (!(a in commandsDistance)) {
98+
commandsDistance[a] = levenshtein(a, commandName);
99+
}
100+
if (!(b in commandsDistance)) {
101+
commandsDistance[b] = levenshtein(b, commandName);
102+
}
103+
104+
return commandsDistance[a] - commandsDistance[b];
105+
});
98106

99-
logger.error(tags.stripIndent`
107+
logger.error(tags.stripIndent`
100108
The specified command ("${commandName}") is invalid. For a list of available options,
101109
run "ng help".
102110
103111
Did you mean "${allCommands[0]}"?
104-
`);
112+
`);
105113

106-
return 1;
114+
return 1;
115+
}
107116
}
108117

109118
const command = new Cmd(context, logger);

0 commit comments

Comments
 (0)