From e5c4455b04e6e63317483c9a1d19990fa9df54d5 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 6 Jul 2021 15:08:55 -0700 Subject: [PATCH 1/2] 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 --- packages/amplify-cli/src/input-manager.ts | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/amplify-cli/src/input-manager.ts b/packages/amplify-cli/src/input-manager.ts index 27d37b9ed1a..06acc408a6d 100644 --- a/packages/amplify-cli/src/input-manager.ts +++ b/packages/amplify-cli/src/input-manager.ts @@ -110,17 +110,41 @@ 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 `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 && input.subCommands && 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 && input.subCommands && Object.keys(commandAliases).includes(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; From 6aee20d0e97544810698cc86914f12fb55eab354 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 9 Jul 2021 15:07:33 -0700 Subject: [PATCH 2/2] refactor(cli): improve validation checks - Array.isArray() and hasOwnProperty() Changed validation checks per requests in PR #7675 --- packages/amplify-cli/src/input-manager.ts | 34 ++++++++++++----------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/amplify-cli/src/input-manager.ts b/packages/amplify-cli/src/input-manager.ts index 06acc408a6d..6a17be6956f 100644 --- a/packages/amplify-cli/src/input-manager.ts +++ b/packages/amplify-cli/src/input-manager.ts @@ -123,25 +123,27 @@ export function verifyInput(pluginPlatform: PluginPlatform, input: Input): Input break; } - // 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 && input.subCommands && commands!.includes(input.subCommands[0])) { - const command = input.subCommands[0]; - input.subCommands[0] = input.command!; - input.command = command; + 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; - } + result.verified = true; + break; + } - // same as above, but check if the first sub-command is an alias. - if (commandAliases && input.subCommands && Object.keys(commandAliases).includes(input.subCommands[0])) { - const command = commandAliases[input.subCommands[0]]; - input.subCommands[0] = input.command!; - input.command = command; + // 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; + result.verified = true; + break; + } } // if `input.command` is the default plugin command, check `input.options` for what to do.