diff --git a/packages/amplify-cli/src/init-steps/s0-analyzeProject.ts b/packages/amplify-cli/src/init-steps/s0-analyzeProject.ts index 95e0ee5a2ef..81e7864dcdf 100644 --- a/packages/amplify-cli/src/init-steps/s0-analyzeProject.ts +++ b/packages/amplify-cli/src/init-steps/s0-analyzeProject.ts @@ -254,8 +254,9 @@ async function getEnvName(context: $TSContext) { await newEnvQuestion(); } else { const allEnvs = context.amplify.getAllEnvs(); + const envAddExec = checkEnvAddExec(context); - if (allEnvs.length > 0) { + if (allEnvs.length > 0 && envAddExec === false) { if (await context.amplify.confirmPrompt('Do you want to use an existing environment?')) { const envQuestion: inquirer.ListQuestion = { type: 'list', @@ -268,6 +269,8 @@ async function getEnvName(context: $TSContext) { } else { await newEnvQuestion(); } + } else if (envAddExec === true && context.parameters.first) { + envName = context.parameters.first; } else { await newEnvQuestion(); } @@ -310,3 +313,12 @@ function getDefaultEditor() { return localEnvInfo.defaultEditor; } + +/** + * Checks if `amplify env add` has been executed + * @param {$TSContext} context The Amplify context object + * @returns `boolean` + */ +function checkEnvAddExec(context): boolean { + return context.parameters.command === 'env' && context.parameters.array[0] === 'add'; +} diff --git a/packages/amplify-e2e-tests/src/environment/env.ts b/packages/amplify-e2e-tests/src/environment/env.ts index ef04cd279b2..bf8f66d891c 100644 --- a/packages/amplify-e2e-tests/src/environment/env.ts +++ b/packages/amplify-e2e-tests/src/environment/env.ts @@ -3,8 +3,6 @@ import { nspawn as spawn, getCLIPath, getSocialProviders, isCI } from 'amplify-e export function addEnvironment(cwd: string, settings: { envName: string; numLayers?: number }): Promise { return new Promise((resolve, reject) => { const chain = spawn(getCLIPath(), ['env', 'add'], { cwd, stripColors: true }) - .wait('Do you want to use an existing environment?') - .sendLine('n') .wait('Enter a name for the environment') .sendLine(settings.envName) .wait('Select the authentication method you want to use:') @@ -58,8 +56,6 @@ export function addEnvironmentYes(cwd: string, settings: { envName: string; disa export function addEnvironmentWithImportedAuth(cwd: string, settings: { envName: string; currentEnvName: string }): Promise { return new Promise((resolve, reject) => { spawn(getCLIPath(), ['env', 'add'], { cwd, stripColors: true }) - .wait('Do you want to use an existing environment?') - .sendConfirmNo() .wait('Enter a name for the environment') .sendLine(settings.envName) .wait('Select the authentication method you want to use:') @@ -179,8 +175,6 @@ export function addEnvironmentHostedUI(cwd: string, settings: { envName: string } = getSocialProviders(); return new Promise((resolve, reject) => { spawn(getCLIPath(), ['env', 'add'], { cwd, stripColors: true }) - .wait('Do you want to use an existing environment?') - .sendLine('n') .wait('Enter a name for the environment') .sendLine(settings.envName) .wait('Select the authentication method you want to use:')