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 20, 2024
1 parent c286c72 commit bbe860f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 70 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
52 changes: 26 additions & 26 deletions packages/eas-cli/src/commands/env/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
EASVariableScopeFlag,
EASVariableVisibilityFlag,
} from '../../commandUtils/flags';
import { EnvironmentVariableScope, EnvironmentVariableVisibility } from '../../graphql/generated';
import {
EnvironmentVariableEnvironment,
EnvironmentVariableScope,
EnvironmentVariableVisibility,
} from '../../graphql/generated';
import { EnvironmentVariableMutation } from '../../graphql/mutations/EnvironmentVariableMutation';
import { EnvironmentVariablesQuery } from '../../graphql/queries/EnvironmentVariablesQuery';
import Log from '../../log';
Expand All @@ -23,7 +27,7 @@ type UpdateFlags = {
name?: string;
value?: string;
scope?: EnvironmentVariableScope;
environment?: string;
environment?: EnvironmentVariableEnvironment;
visibility?: EnvironmentVariableVisibility;
'non-interactive': boolean;
};
Expand Down Expand Up @@ -76,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 @@ -112,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 @@ -165,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

Check failure on line 50 in packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts

View workflow job for this annotation

GitHub Actions / Test with Node 18

Variable "$environment" of type "EnvironmentVariableEnvironment" used in position expecting type "EnvironmentVariableEnvironment!"

Check failure on line 50 in packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts

View workflow job for this annotation

GitHub Actions / Test with Node 22

Variable "$environment" of type "EnvironmentVariableEnvironment" used in position expecting type "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

Check failure on line 85 in packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts

View workflow job for this annotation

GitHub Actions / Test with Node 18

Variable "$environment" of type "EnvironmentVariableEnvironment" used in position expecting type "EnvironmentVariableEnvironment!"

Check failure on line 85 in packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts

View workflow job for this annotation

GitHub Actions / Test with Node 22

Variable "$environment" of type "EnvironmentVariableEnvironment" used in position expecting type "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!) {

Check failure on line 177 in packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts

View workflow job for this annotation

GitHub Actions / Test with Node 18

Unknown type "UpdateEnvironmentVariableInput". Did you mean "CreateEnvironmentVariableInput", "DeleteEnvironmentVariableResult", "LinkSharedEnvironmentVariableInput", "CreateSharedEnvironmentVariableInput", or "CreateEnvironmentSecretInput"?

Check failure on line 177 in packages/eas-cli/src/graphql/mutations/EnvironmentVariableMutation.ts

View workflow job for this annotation

GitHub Actions / Test with Node 22

Unknown type "UpdateEnvironmentVariableInput". Did you mean "CreateEnvironmentVariableInput", "DeleteEnvironmentVariableResult", "LinkSharedEnvironmentVariableInput", "CreateSharedEnvironmentVariableInput", or "CreateEnvironmentSecretInput"?
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 bbe860f

Please sign in to comment.