From 8668cc991946d4bf9612f08bf240504c7317167d Mon Sep 17 00:00:00 2001 From: Piotr Szeremeta Date: Wed, 18 Sep 2024 14:08:07 +0200 Subject: [PATCH] Allow prompting for multiple environments --- packages/eas-cli/src/commandUtils/flags.ts | 23 +++++++--- packages/eas-cli/src/commands/env/create.ts | 2 +- packages/eas-cli/src/commands/env/delete.ts | 2 +- packages/eas-cli/src/commands/env/exec.ts | 3 +- packages/eas-cli/src/commands/env/get.ts | 2 +- packages/eas-cli/src/commands/env/link.ts | 2 +- packages/eas-cli/src/commands/env/list.ts | 2 +- packages/eas-cli/src/commands/env/pull.ts | 2 +- packages/eas-cli/src/commands/env/unlink.ts | 2 +- packages/eas-cli/src/commands/env/update.ts | 2 +- packages/eas-cli/src/utils/prompts.ts | 49 +++++++++++++++++---- 11 files changed, 67 insertions(+), 24 deletions(-) diff --git a/packages/eas-cli/src/commandUtils/flags.ts b/packages/eas-cli/src/commandUtils/flags.ts index a586e99310..0287437ef8 100644 --- a/packages/eas-cli/src/commandUtils/flags.ts +++ b/packages/eas-cli/src/commandUtils/flags.ts @@ -26,15 +26,24 @@ export const EasNonInteractiveAndJsonFlags = { }), }; +const EasEnvironmentFlagParameters = { + description: "Environment variable's environment", + parse: upperCaseAsync, + options: mapToLowercase([ + EnvironmentVariableEnvironment.Development, + EnvironmentVariableEnvironment.Preview, + EnvironmentVariableEnvironment.Production, + ]), +}; + export const EASEnvironmentFlag = { + environment: Flags.enum(EasEnvironmentFlagParameters), +}; + +export const EASMultiEnvironmentFlag = { environment: Flags.enum({ - description: "Environment variable's environment", - parse: upperCaseAsync, - options: mapToLowercase([ - EnvironmentVariableEnvironment.Development, - EnvironmentVariableEnvironment.Preview, - EnvironmentVariableEnvironment.Production, - ]), + ...EasEnvironmentFlagParameters, + multiple: true, }), }; diff --git a/packages/eas-cli/src/commands/env/create.ts b/packages/eas-cli/src/commands/env/create.ts index 22af8b1bdf..e07039a2e6 100644 --- a/packages/eas-cli/src/commands/env/create.ts +++ b/packages/eas-cli/src/commands/env/create.ts @@ -112,7 +112,7 @@ export default class EnvironmentVariableCreate extends EasCommand { } if (!environment) { - environment = await promptVariableEnvironmentAsync(nonInteractive); + environment = await promptVariableEnvironmentAsync({ nonInteractive }); } const environments = [environment]; diff --git a/packages/eas-cli/src/commands/env/delete.ts b/packages/eas-cli/src/commands/env/delete.ts index 66c388283e..d47935fdc0 100644 --- a/packages/eas-cli/src/commands/env/delete.ts +++ b/packages/eas-cli/src/commands/env/delete.ts @@ -52,7 +52,7 @@ export default class EnvironmentVariableDelete extends EasCommand { if (scope === EnvironmentVariableScope.Project) { if (!environment) { - environment = await promptVariableEnvironmentAsync(nonInteractive); + environment = await promptVariableEnvironmentAsync({ nonInteractive }); } } diff --git a/packages/eas-cli/src/commands/env/exec.ts b/packages/eas-cli/src/commands/env/exec.ts index b91d77cdde..7cbfac3b76 100644 --- a/packages/eas-cli/src/commands/env/exec.ts +++ b/packages/eas-cli/src/commands/env/exec.ts @@ -64,7 +64,8 @@ export default class EnvExec extends EasCommand { }); const environment = - parsedFlags.environment ?? (await promptVariableEnvironmentAsync(parsedFlags.nonInteractive)); + parsedFlags.environment ?? + (await promptVariableEnvironmentAsync({ nonInteractive: parsedFlags.nonInteractive })); const environmentVariables = await this.loadEnvironmentVariablesAsync({ graphqlClient, projectId, diff --git a/packages/eas-cli/src/commands/env/get.ts b/packages/eas-cli/src/commands/env/get.ts index 6db4dbe747..bcfdfbc19a 100644 --- a/packages/eas-cli/src/commands/env/get.ts +++ b/packages/eas-cli/src/commands/env/get.ts @@ -70,7 +70,7 @@ export default class EnvironmentVariableGet extends EasCommand { } if (!environment && scope === EnvironmentVariableScope.Project) { - environment = await promptVariableEnvironmentAsync(nonInteractive); + environment = await promptVariableEnvironmentAsync({ nonInteractive }); } const variable = await getVariableAsync(graphqlClient, scope, projectId, name, environment); diff --git a/packages/eas-cli/src/commands/env/link.ts b/packages/eas-cli/src/commands/env/link.ts index c325b9552e..c9445d8657 100644 --- a/packages/eas-cli/src/commands/env/link.ts +++ b/packages/eas-cli/src/commands/env/link.ts @@ -61,7 +61,7 @@ export default class EnvironmentVariableLink extends EasCommand { } if (!environment) { - environment = await promptVariableEnvironmentAsync(nonInteractive); + environment = await promptVariableEnvironmentAsync({ nonInteractive }); } const linkedVariable = await EnvironmentVariableMutation.linkSharedEnvironmentVariableAsync( diff --git a/packages/eas-cli/src/commands/env/list.ts b/packages/eas-cli/src/commands/env/list.ts index 39a0a3bd99..b5fd7875d7 100644 --- a/packages/eas-cli/src/commands/env/list.ts +++ b/packages/eas-cli/src/commands/env/list.ts @@ -50,7 +50,7 @@ export default class EnvironmentValueList extends EasCommand { }); if (scope === EnvironmentVariableScope.Project && !environment) { - environment = await promptVariableEnvironmentAsync(false); + environment = await promptVariableEnvironmentAsync({ nonInteractive: false }); } const variables = await this.getVariablesForScopeAsync(graphqlClient, { diff --git a/packages/eas-cli/src/commands/env/pull.ts b/packages/eas-cli/src/commands/env/pull.ts index 5c88940131..debbcbc3a5 100644 --- a/packages/eas-cli/src/commands/env/pull.ts +++ b/packages/eas-cli/src/commands/env/pull.ts @@ -34,7 +34,7 @@ export default class EnvironmentVariablePull extends EasCommand { } = await this.parse(EnvironmentVariablePull); if (!environment) { - environment = await promptVariableEnvironmentAsync(nonInteractive); + environment = await promptVariableEnvironmentAsync({ nonInteractive }); } const { privateProjectConfig: { projectId }, diff --git a/packages/eas-cli/src/commands/env/unlink.ts b/packages/eas-cli/src/commands/env/unlink.ts index 85640e5ee2..8176ab69ba 100644 --- a/packages/eas-cli/src/commands/env/unlink.ts +++ b/packages/eas-cli/src/commands/env/unlink.ts @@ -41,7 +41,7 @@ export default class EnvironmentVariableUnlink extends EasCommand { }); if (!environment) { - environment = await promptVariableEnvironmentAsync(nonInteractive); + environment = await promptVariableEnvironmentAsync({ nonInteractive }); } const projectDisplayName = await getDisplayNameForProjectIdAsync(graphqlClient, projectId); diff --git a/packages/eas-cli/src/commands/env/update.ts b/packages/eas-cli/src/commands/env/update.ts index f345b8bf81..74f46f262a 100644 --- a/packages/eas-cli/src/commands/env/update.ts +++ b/packages/eas-cli/src/commands/env/update.ts @@ -81,7 +81,7 @@ export default class EnvironmentVariableUpdate extends EasCommand { ]); if (!environment) { - environment = await promptVariableEnvironmentAsync(nonInteractive); + environment = await promptVariableEnvironmentAsync({ nonInteractive }); } const environments = environment ? [environment] : undefined; diff --git a/packages/eas-cli/src/utils/prompts.ts b/packages/eas-cli/src/utils/prompts.ts index 64b0a66561..db4a4e15fe 100644 --- a/packages/eas-cli/src/utils/prompts.ts +++ b/packages/eas-cli/src/utils/prompts.ts @@ -3,19 +3,52 @@ import chalk from 'chalk'; import { EnvironmentVariableEnvironment } from '../graphql/generated'; import { promptAsync, selectAsync } from '../prompts'; -export async function promptVariableEnvironmentAsync( - nonInteractive: boolean -): Promise { +type EnvironmentPromptArgs = { + nonInteractive: boolean; + selectedEnvironments?: EnvironmentVariableEnvironment[]; + availableEnvironments?: EnvironmentVariableEnvironment[]; +}; + +export function promptVariableEnvironmentAsync( + input: EnvironmentPromptArgs & { multiple: true } +): Promise; +export function promptVariableEnvironmentAsync( + input: EnvironmentPromptArgs & { multiple?: false } +): Promise; + +export async function promptVariableEnvironmentAsync({ + nonInteractive, + selectedEnvironments, + multiple = false, + availableEnvironments, +}: EnvironmentPromptArgs & { multiple?: boolean }): Promise< + EnvironmentVariableEnvironment[] | EnvironmentVariableEnvironment +> { if (nonInteractive) { throw new Error( 'The `--environment` flag must be set when running in `--non-interactive` mode.' ); } - return await selectAsync('Select environment:', [ - { title: 'development', value: EnvironmentVariableEnvironment.Development }, - { title: 'preview', value: EnvironmentVariableEnvironment.Preview }, - { title: 'production', value: EnvironmentVariableEnvironment.Production }, - ]); + if (!multiple) { + return await selectAsync( + 'Select environment:', + (availableEnvironments ?? Object.values(EnvironmentVariableEnvironment)).map(environment => ({ + title: environment, + value: environment, + })) + ); + } + const { environments } = await promptAsync({ + message: 'Select environment:', + name: 'environments', + type: 'multiselect', + choices: Object.values(EnvironmentVariableEnvironment).map(environment => ({ + title: environment, + value: environment, + selected: selectedEnvironments?.includes(environment), + })), + }); + return environments; } export async function promptVariableValueAsync({