Skip to content

Commit

Permalink
Update EnvironmentVariableMutation and associated commands
Browse files Browse the repository at this point in the history
  • Loading branch information
khamilowicz committed Sep 23, 2024
1 parent 0b07b3f commit f315c41
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 68 deletions.
46 changes: 29 additions & 17 deletions packages/eas-cli/src/commands/env/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
EASVariableVisibilityFlag,
} from '../../commandUtils/flags';
import {
EnvironmentSecretType,
EnvironmentVariableEnvironment,
EnvironmentVariableScope,
EnvironmentVariableVisibility,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}`
Expand All @@ -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.`
);
}

Expand All @@ -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
);
Expand Down
44 changes: 20 additions & 24 deletions packages/eas-cli/src/commands/env/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}`
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 9 additions & 2 deletions packages/eas-cli/src/graphql/generated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -35,15 +39,15 @@ export const EnvironmentVariableMutation = {
graphqlClient: ExpoGraphqlClient,
environmentVariableId: string,
appId: string,
environment: string
environment?: EnvironmentVariableEnvironment
): Promise<EnvironmentVariableFragment> {
const data = await withErrorHandlingAsync(
graphqlClient
.mutation<LinkSharedEnvironmentVariableMutation>(
gql`
mutation LinkSharedEnvironmentVariable(
$appId: ID!
$environment: EnvironmentVariableEnvironment!
$environment: EnvironmentVariableEnvironment
$environmentVariableId: ID!
) {
environmentVariable {
Expand All @@ -70,15 +74,15 @@ export const EnvironmentVariableMutation = {
graphqlClient: ExpoGraphqlClient,
environmentVariableId: string,
appId: string,
environment: string
environment?: EnvironmentVariableEnvironment
): Promise<EnvironmentVariableFragment> {
const data = await withErrorHandlingAsync(
graphqlClient
.mutation<UnlinkSharedEnvironmentVariableMutation>(
gql`
mutation UnlinkSharedEnvironmentVariable(
$appId: ID!
$environment: EnvironmentVariableEnvironment!
$environment: EnvironmentVariableEnvironment
$environmentVariableId: ID!
) {
environmentVariable {
Expand All @@ -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<EnvironmentVariableFragment> {
const data = await withErrorHandlingAsync(
Expand Down Expand Up @@ -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<EnvironmentVariableFragment> {
const data = await withErrorHandlingAsync(
Expand All @@ -177,6 +166,31 @@ export const EnvironmentVariableMutation = {

return data.environmentVariable.createEnvironmentVariableForApp;
},
async updateAsync(
graphqlClient: ExpoGraphqlClient,
input: Partial<CreateVariableArgs> & { id: string }
): Promise<EnvironmentVariableFragment> {
const data = await withErrorHandlingAsync(
graphqlClient
.mutation<UpdateEnvironmentVariableMutation>(
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
Expand Down

0 comments on commit f315c41

Please sign in to comment.