Skip to content

Improve invalid command message #10260

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 2 commits into from
Apr 10, 2018
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
50 changes: 27 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 67 additions & 5 deletions packages/@angular/cli/models/command-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,58 @@ export async function runCommand(commandMap: CommandMap,
}

if (!Cmd) {
logger.error(tags.oneLine`The specified command (${commandName}) is invalid.
For a list of available options, run \`ng help\`.`);
throw '';
function levenshtein(a: string, b: string): number {
if (a.length === 0) {
return b.length;
}
if (b.length === 0) {
return a.length;
}

if (a.length > b.length) {
let tmp = a;
a = b;
b = tmp;
}

const row = Array(a.length);
for (let i = 0; i <= a.length; i++) {
row[i] = i;
}

let result: number;
for (let i = 1; i <= b.length; i++) {
result = i;

for (let j = 1; j <= a.length; j++) {
let tmp = row[j - 1];
row[j - 1] = result;
result = b[i - 1] === a[j - 1]
? tmp
: Math.min(tmp + 1, Math.min(result + 1, row[j] + 1));
}
}

return result;
}

const commandsDistance = {} as { [name: string]: number };
const allCommands = listAllCommandNames(commandMap).sort((a, b) => {
if (!(a in commandsDistance)) {
commandsDistance[a] = levenshtein(a, commandName);
}
if (!(b in commandsDistance)) {
commandsDistance[b] = levenshtein(b, commandName);
}
return commandsDistance[a] - commandsDistance[b];
});

throw new SilentError(tags.stripIndent`
The specified command ("${commandName}") is invalid. For a list of available options,
run "ng help".

Did you mean "${allCommands[0]}"?
`);
}

const command = new Cmd(context, logger);
Expand Down Expand Up @@ -167,8 +216,7 @@ export function parseOptions<T = any>(
}

// Find a command.
function findCommand(
map: CommandMap, name: string): CommandConstructor | null {
function findCommand(map: CommandMap, name: string): CommandConstructor | null {
let Cmd: CommandConstructor = map[name];

if (!Cmd) {
Expand All @@ -194,6 +242,20 @@ function findCommand(
return Cmd;
}

function listAllCommandNames(map: CommandMap): string[] {
return Object.keys(map).concat(
Object.keys(map)
.reduce((acc, key) => {
if (!map[key].aliases) {
return acc;
}

return acc.concat(map[key].aliases);
}, [] as string[]),
);
}


function verifyCommandInScope(command: Command, scope = CommandScope.everywhere): void {
if (!command) {
return;
Expand Down