Skip to content

Commit

Permalink
Allow prompting for multiple environments
Browse files Browse the repository at this point in the history
  • Loading branch information
khamilowicz committed Sep 23, 2024
1 parent d42cbe0 commit 4fe62cc
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 27 deletions.
23 changes: 16 additions & 7 deletions packages/eas-cli/src/commandUtils/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EnvironmentVariableEnvironment>(EasEnvironmentFlagParameters),
};

export const EASMultiEnvironmentFlag = {
environment: Flags.enum<EnvironmentVariableEnvironment>({
description: "Environment variable's environment",
parse: upperCaseAsync,
options: mapToLowercase([
EnvironmentVariableEnvironment.Development,
EnvironmentVariableEnvironment.Preview,
EnvironmentVariableEnvironment.Production,
]),
...EasEnvironmentFlagParameters,
multiple: true,
}),
};

Expand Down
9 changes: 6 additions & 3 deletions packages/eas-cli/src/commands/env/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ export default class EnvironmentVariableCreate extends EasCommand {
});
}

if (!environment) {
environment = await promptVariableEnvironmentAsync({ nonInteractive });
}

const environments = [environment];

if (scope === EnvironmentVariableScope.Project) {
if (!environment) {
environment = await promptVariableEnvironmentAsync(nonInteractive);
}
const existingVariables = await EnvironmentVariablesQuery.byAppIdAsync(graphqlClient, {
appId: projectId,
environment,
Expand Down
2 changes: 1 addition & 1 deletion packages/eas-cli/src/commands/env/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class EnvironmentVariableDelete extends EasCommand {

if (scope === EnvironmentVariableScope.Project) {
if (!environment) {
environment = await promptVariableEnvironmentAsync(nonInteractive);
environment = await promptVariableEnvironmentAsync({ nonInteractive });
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/eas-cli/src/commands/env/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/eas-cli/src/commands/env/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/eas-cli/src/commands/env/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion packages/eas-cli/src/commands/env/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion packages/eas-cli/src/commands/env/unlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions packages/eas-cli/src/commands/env/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ export default class EnvironmentVariableUpdate extends EasCommand {
getOwnerAccountForProjectIdAsync(graphqlClient, projectId),
]);

if (!environment) {
environment = await promptVariableEnvironmentAsync({ nonInteractive });
}

const environments = environment ? [environment] : undefined;

if (scope === EnvironmentVariableScope.Project) {
if (!environment) {
environment = await promptVariableEnvironmentAsync(nonInteractive);
}
const existingVariables = await EnvironmentVariablesQuery.byAppIdAsync(graphqlClient, {
appId: projectId,
environment,
Expand Down
48 changes: 40 additions & 8 deletions packages/eas-cli/src/utils/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
import chalk from 'chalk';

import capitalize from './expodash/capitalize';
import { EnvironmentVariableEnvironment } from '../graphql/generated';
import { promptAsync, selectAsync } from '../prompts';

export async function promptVariableEnvironmentAsync(
nonInteractive: boolean
): Promise<EnvironmentVariableEnvironment> {
type EnvironmentPromptArgs = {
nonInteractive: boolean;
selectedEnvironments?: EnvironmentVariableEnvironment[];
};

export function promptVariableEnvironmentAsync(
input: EnvironmentPromptArgs & { multiple: true }
): Promise<EnvironmentVariableEnvironment[]>;
export function promptVariableEnvironmentAsync(
input: EnvironmentPromptArgs & { multiple?: false }
): Promise<EnvironmentVariableEnvironment>;

export async function promptVariableEnvironmentAsync({
nonInteractive,
selectedEnvironments,
multiple = false,
}: 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:',
Object.values(EnvironmentVariableEnvironment).map(environment => ({
title: capitalize(environment),
value: environment,
}))
);
}
const { environments } = await promptAsync({
message: 'Select environment:',
name: 'environments',
type: 'multiselect',
choices: Object.values(EnvironmentVariableEnvironment).map(environment => ({
title: capitalize(environment),
value: environment,
selected: selectedEnvironments?.includes(environment),
})),
});
return environments;
}
export async function promptVariableValueAsync({
nonInteractive,
Expand Down

0 comments on commit 4fe62cc

Please sign in to comment.