Skip to content

Commit

Permalink
feat(cli): add consistent support for verb / noun swapping (#7675)
Browse files Browse the repository at this point in the history
* feat(cli): add consistent support for verb / noun swapping

commands in the 'core' plugin that contain sub-commands (env and plugin) now support verb / noun
swapping (`env add` and `add env` now both work)

close #7630

* refactor(cli): improve validation checks - Array.isArray() and hasOwnProperty()

Changed validation checks per requests in PR #7675
  • Loading branch information
Bentheburrito authored and jhockett committed Aug 25, 2021
1 parent 5b7a79e commit f4991ce
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/amplify-cli/src/input-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,43 @@ export function verifyInput(pluginPlatform: PluginPlatform, input: Input): Input
result.helpCommandAvailable = true;
}

// verify if `input.command` is an actual command.
if (commands && commands!.includes(input.command!)) {
result.verified = true;
break;
}

// verify if `input.command` is an alias for a command.
if (commandAliases && Object.keys(commandAliases).includes(input.command!)) {
input.command = commandAliases[input.command!];
result.verified = true;
break;
}

if (Array.isArray(input.subCommands) && input.subCommands.length > 0) {
// if `input.command` is not a command name or an alias for a command, check the
// first sub-command for a verb / noun swap (i.e. `env add` versus `add env`).
if (commands && commands!.includes(input.subCommands[0])) {
const command = input.subCommands[0];
input.subCommands[0] = input.command!;
input.command = command;

result.verified = true;
break;
}

// same as above, but check if the first sub-command is an alias.
if (commandAliases && commandAliases.hasOwnProperty(input.subCommands[0])) {
const command = commandAliases[input.subCommands[0]];
input.subCommands[0] = input.command!;
input.command = command;

result.verified = true;
break;
}
}

// if `input.command` is the default plugin command, check `input.options` for what to do.
if (input.command! === constants.PLUGIN_DEFAULT_COMMAND) {
if (commands && commands!.includes(name)) {
input.command = name;
Expand Down

0 comments on commit f4991ce

Please sign in to comment.