From df6b8c1b6db1ede9144aec41e8cfb6a5fbc75aed Mon Sep 17 00:00:00 2001 From: Piotr Szeremeta Date: Tue, 17 Sep 2024 13:14:21 +0200 Subject: [PATCH] Update EnvironmentVariableMutation and associated commands --- packages/eas-cli/src/commands/env/create.ts | 46 ++++++++----- packages/eas-cli/src/commands/env/update.ts | 44 ++++++------- packages/eas-cli/src/graphql/generated.ts | 11 +++- .../mutations/EnvironmentVariableMutation.ts | 64 +++++++++++-------- 4 files changed, 97 insertions(+), 68 deletions(-) diff --git a/packages/eas-cli/src/commands/env/create.ts b/packages/eas-cli/src/commands/env/create.ts index a40d0134d5..22af8b1bdf 100644 --- a/packages/eas-cli/src/commands/env/create.ts +++ b/packages/eas-cli/src/commands/env/create.ts @@ -9,6 +9,7 @@ import { EASVariableVisibilityFlag, } from '../../commandUtils/flags'; import { + EnvironmentSecretType, EnvironmentVariableEnvironment, EnvironmentVariableScope, EnvironmentVariableVisibility, @@ -110,10 +111,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, @@ -168,18 +172,28 @@ export default class EnvironmentVariableCreate extends EasCommand { overwrite = true; } } - - const variable = await EnvironmentVariableMutation.createForAppAsync( - graphqlClient, - { + let variable; + if (overwrite && existingVariable) { + variable = await EnvironmentVariableMutation.updateAsync(graphqlClient, { + id: existingVariable.id, name, value, - environment, visibility, - overwrite, - }, - projectId - ); + environments, + }); + } else { + variable = await EnvironmentVariableMutation.createForAppAsync( + graphqlClient, + { + name, + value, + environments, + visibility, + type: EnvironmentSecretType.String, + }, + projectId + ); + } if (!variable) { throw new Error( `Could not create variable with name ${name} on project ${projectDisplayName}` @@ -197,7 +211,7 @@ export default class EnvironmentVariableCreate extends EasCommand { if (existingVariable) { throw new Error( `Shared variable with ${name} name already exists on this account.\n` + - `Use a different name or delete the existing variable on website or by using eas env:delete --name ${name} --scope shared command.` + `Use a different name or delete the existing variable on website or by using eas env:delete --name ${name} --scope shared command.` ); } @@ -212,16 +226,14 @@ export default class EnvironmentVariableCreate extends EasCommand { } } - if (!environment && link) { - environment = await promptVariableEnvironmentAsync(nonInteractive); - } - const variable = await EnvironmentVariableMutation.createSharedVariableAsync( graphqlClient, { name, value, visibility, + environments, + type: EnvironmentSecretType.String, }, ownerAccount.id ); diff --git a/packages/eas-cli/src/commands/env/update.ts b/packages/eas-cli/src/commands/env/update.ts index 719a98b151..f345b8bf81 100644 --- a/packages/eas-cli/src/commands/env/update.ts +++ b/packages/eas-cli/src/commands/env/update.ts @@ -80,10 +80,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, @@ -116,17 +119,13 @@ export default class EnvironmentVariableUpdate extends EasCommand { } } - const variable = await EnvironmentVariableMutation.createForAppAsync( - graphqlClient, - { - name, - value, - environment, - visibility, - overwrite: true, - }, - projectId - ); + const variable = await EnvironmentVariableMutation.updateAsync(graphqlClient, { + id: existingVariable.id, + name, + value, + environments, + visibility, + }); if (!variable) { throw new Error( `Could not update variable with name ${name} on project ${projectDisplayName}` @@ -169,16 +168,13 @@ export default class EnvironmentVariableUpdate extends EasCommand { } } - const variable = await EnvironmentVariableMutation.createSharedVariableAsync( - graphqlClient, - { - name, - value, - visibility, - overwrite: true, - }, - ownerAccount.id - ); + const variable = await EnvironmentVariableMutation.updateAsync(graphqlClient, { + id: existingVariable.id, + name, + value, + visibility, + environments, + }); if (!variable) { throw new Error( diff --git a/packages/eas-cli/src/graphql/generated.ts b/packages/eas-cli/src/graphql/generated.ts index 22786cdc4a..634f8afe5e 100644 --- a/packages/eas-cli/src/graphql/generated.ts +++ b/packages/eas-cli/src/graphql/generated.ts @@ -8425,7 +8425,7 @@ export type DeleteEnvironmentSecretMutation = { __typename?: 'RootMutation', env export type LinkSharedEnvironmentVariableMutationVariables = Exact<{ appId: Scalars['ID']['input']; - environment: EnvironmentVariableEnvironment; + environment?: InputMaybe; environmentVariableId: Scalars['ID']['input']; }>; @@ -8434,7 +8434,7 @@ export type LinkSharedEnvironmentVariableMutation = { __typename?: 'RootMutation export type UnlinkSharedEnvironmentVariableMutationVariables = Exact<{ appId: Scalars['ID']['input']; - environment: EnvironmentVariableEnvironment; + environment?: InputMaybe; environmentVariableId: Scalars['ID']['input']; }>; @@ -8457,6 +8457,13 @@ export type CreateEnvironmentVariableForAppMutationVariables = Exact<{ export type CreateEnvironmentVariableForAppMutation = { __typename?: 'RootMutation', environmentVariable: { __typename?: 'EnvironmentVariableMutation', createEnvironmentVariableForApp: { __typename?: 'EnvironmentVariable', id: string, name: string, value?: string | null, environment?: EnvironmentVariableEnvironment | null, createdAt: any, scope: EnvironmentVariableScope, visibility?: EnvironmentVariableVisibility | null } } }; +export type UpdateEnvironmentVariableMutationVariables = Exact<{ + input: UpdateEnvironmentVariableInput; +}>; + + +export type UpdateEnvironmentVariableMutation = { __typename?: 'RootMutation', environmentVariable: { __typename?: 'EnvironmentVariableMutation', updateEnvironmentVariable: { __typename?: 'EnvironmentVariable', id: string, name: string, value?: string | null, environment?: EnvironmentVariableEnvironment | null, createdAt: any, scope: EnvironmentVariableScope, visibility?: EnvironmentVariableVisibility | null } } }; + export type DeleteEnvironmentVariableMutationVariables = Exact<{ id: Scalars['ID']['input']; }>; diff --git a/packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts b/packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts index 902c124ade..363870e6a3 100644 --- a/packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts +++ b/packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts @@ -8,18 +8,22 @@ import { CreateEnvironmentVariableForAccountMutation, CreateEnvironmentVariableForAppMutation, DeleteEnvironmentVariableMutation, + EnvironmentSecretType, + EnvironmentVariableEnvironment, EnvironmentVariableFragment, EnvironmentVariableVisibility, LinkSharedEnvironmentVariableMutation, UnlinkSharedEnvironmentVariableMutation, + UpdateEnvironmentVariableMutation, } from '../generated'; import { EnvironmentVariableFragmentNode } from '../types/EnvironmentVariable'; -type UpdateVariableArgs = { - value?: string; +type CreateVariableArgs = { + value: string; name: string; - overwrite: true; - visibility?: EnvironmentVariableVisibility; + visibility: EnvironmentVariableVisibility; + environments: EnvironmentVariableEnvironment[]; + type: EnvironmentSecretType; }; export type EnvironmentVariablePushInput = { @@ -35,7 +39,7 @@ export const EnvironmentVariableMutation = { graphqlClient: ExpoGraphqlClient, environmentVariableId: string, appId: string, - environment: string + environment?: EnvironmentVariableEnvironment ): Promise { const data = await withErrorHandlingAsync( graphqlClient @@ -43,7 +47,7 @@ export const EnvironmentVariableMutation = { gql` mutation LinkSharedEnvironmentVariable( $appId: ID! - $environment: EnvironmentVariableEnvironment! + $environment: EnvironmentVariableEnvironment $environmentVariableId: ID! ) { environmentVariable { @@ -70,7 +74,7 @@ export const EnvironmentVariableMutation = { graphqlClient: ExpoGraphqlClient, environmentVariableId: string, appId: string, - environment: string + environment?: EnvironmentVariableEnvironment ): Promise { const data = await withErrorHandlingAsync( graphqlClient @@ -78,7 +82,7 @@ export const EnvironmentVariableMutation = { gql` mutation UnlinkSharedEnvironmentVariable( $appId: ID! - $environment: EnvironmentVariableEnvironment! + $environment: EnvironmentVariableEnvironment $environmentVariableId: ID! ) { environmentVariable { @@ -103,14 +107,7 @@ export const EnvironmentVariableMutation = { }, async createSharedVariableAsync( graphqlClient: ExpoGraphqlClient, - input: - | { - name: string; - value: string; - visibility: EnvironmentVariableVisibility; - overwrite?: boolean; - } - | UpdateVariableArgs, + input: CreateVariableArgs, accountId: string ): Promise { const data = await withErrorHandlingAsync( @@ -142,15 +139,7 @@ export const EnvironmentVariableMutation = { }, async createForAppAsync( graphqlClient: ExpoGraphqlClient, - input: - | { - name: string; - value?: string; - environment: string; - visibility: EnvironmentVariableVisibility; - overwrite?: boolean; - } - | (UpdateVariableArgs & { environment: string }), + input: CreateVariableArgs, appId: string ): Promise { const data = await withErrorHandlingAsync( @@ -177,6 +166,31 @@ export const EnvironmentVariableMutation = { return data.environmentVariable.createEnvironmentVariableForApp; }, + async updateAsync( + graphqlClient: ExpoGraphqlClient, + input: Partial & { id: string } + ): Promise { + const data = await withErrorHandlingAsync( + graphqlClient + .mutation( + gql` + mutation UpdateEnvironmentVariable($input: UpdateEnvironmentVariableInput!) { + environmentVariable { + updateEnvironmentVariable(environmentVariableData: $input) { + id + ...EnvironmentVariableFragment + } + } + } + ${print(EnvironmentVariableFragmentNode)} + `, + { input } + ) + .toPromise() + ); + + return data.environmentVariable.updateEnvironmentVariable; + }, async deleteAsync(graphqlClient: ExpoGraphqlClient, id: string): Promise<{ id: string }> { const data = await withErrorHandlingAsync( graphqlClient