Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This is the log of notable changes to EAS CLI and related packages.

### 🎉 New features

- [eas-cli] Add `eas update:embedded:delete` command. ([@gwdp](https://github.com/gwdp))
- [build-tools] Auto-upload embedded bundle after build when `EAS_UPDATE_EXPERIMENTAL_UPLOAD_EMBEDDED_BUNDLE` is set. ([#3767](https://github.com/expo/eas-cli/pull/3767) by [@gwdp](https://github.com/gwdp))

### 🐛 Bug fixes
Expand Down
103 changes: 103 additions & 0 deletions packages/eas-cli/src/commands/update/embedded/__tests__/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { CombinedError } from '@urql/core';

import { getMockOclifConfig } from '../../../../__tests__/commands/utils';
import { ExpoGraphqlClient } from '../../../../commandUtils/context/contextUtils/createGraphqlClient';
import {
EmbeddedUpdateMutation,
isEmbeddedUpdateNotFoundError,
} from '../../../../graphql/mutations/EmbeddedUpdateMutation';
import Log from '../../../../log';
import * as prompts from '../../../../prompts';
import * as json from '../../../../utils/json';
import UpdateEmbeddedDelete from '../delete';

jest.mock('../../../../graphql/mutations/EmbeddedUpdateMutation', () => ({
EmbeddedUpdateMutation: { deleteEmbeddedUpdateAsync: jest.fn() },
isEmbeddedUpdateNotFoundError: jest.fn(),
}));
jest.mock('../../../../log');
jest.mock('../../../../utils/json');
jest.mock('../../../../prompts');

const mockDelete = jest.mocked(EmbeddedUpdateMutation.deleteEmbeddedUpdateAsync);
const mockIsNotFound = jest.mocked(isEmbeddedUpdateNotFoundError);
const mockConfirm = jest.mocked(prompts.confirmAsync);
const mockLogWithTick = jest.mocked(Log.withTick);
const mockEnableJsonOutput = jest.mocked(json.enableJsonOutput);
const mockPrintJson = jest.mocked(json.printJsonOnlyOutput);

const VALID_UUID = 'a1b2c3d4-1234-4000-8000-000000000000';

const MOCK_CONTEXT = {
loggedIn: { graphqlClient: {} as ExpoGraphqlClient },
};

describe(UpdateEmbeddedDelete, () => {
const mockConfig = getMockOclifConfig();

beforeEach(() => {
jest.clearAllMocks();
mockDelete.mockResolvedValue({ id: VALID_UUID });
mockIsNotFound.mockReturnValue(false);
mockConfirm.mockResolvedValue(true);
});

function createCommand(argv: string[]): UpdateEmbeddedDelete {
const command = new UpdateEmbeddedDelete(argv, mockConfig);
// @ts-expect-error getContextAsync is protected
jest.spyOn(command, 'getContextAsync').mockResolvedValue(MOCK_CONTEXT);
return command;
}

it('deletes when the user confirms', async () => {
await createCommand([VALID_UUID]).run();

expect(mockConfirm).toHaveBeenCalledTimes(1);
expect(mockDelete).toHaveBeenCalledWith(MOCK_CONTEXT.loggedIn.graphqlClient, {
id: VALID_UUID,
});
expect(mockLogWithTick).toHaveBeenCalledWith(`Deleted embedded update ${VALID_UUID}`);
});

it('skips confirmation in non-interactive mode', async () => {
await createCommand([VALID_UUID, '--non-interactive']).run();

expect(mockConfirm).not.toHaveBeenCalled();
expect(mockDelete).toHaveBeenCalledWith(MOCK_CONTEXT.loggedIn.graphqlClient, {
id: VALID_UUID,
});
});

it('aborts without calling the mutation when the user declines', async () => {
mockConfirm.mockResolvedValue(false);
await createCommand([VALID_UUID]).run();

expect(mockDelete).not.toHaveBeenCalled();
expect(mockLogWithTick).not.toHaveBeenCalled();
});

it('--json prints { id } and skips the human-readable success line', async () => {
await createCommand([VALID_UUID, '--non-interactive', '--json']).run();

expect(mockEnableJsonOutput).toHaveBeenCalled();
expect(mockPrintJson).toHaveBeenCalledWith({ id: VALID_UUID });
expect(mockLogWithTick).not.toHaveBeenCalled();
});

it('exits with a friendly message when the server returns NOT_FOUND', async () => {
const notFound = new CombinedError({ graphQLErrors: [] });
mockDelete.mockRejectedValue(notFound);
mockIsNotFound.mockReturnValue(true);

await expect(createCommand([VALID_UUID, '--non-interactive']).run()).rejects.toThrow();
expect(mockIsNotFound).toHaveBeenCalledWith(notFound);
});

it('rethrows unexpected errors', async () => {
const boom = new Error('boom');
mockDelete.mockRejectedValue(boom);
mockIsNotFound.mockReturnValue(false);

await expect(createCommand([VALID_UUID, '--non-interactive']).run()).rejects.toThrow('boom');
});
});
86 changes: 86 additions & 0 deletions packages/eas-cli/src/commands/update/embedded/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Args, Errors } from '@oclif/core';
import chalk from 'chalk';

import EasCommand from '../../../commandUtils/EasCommand';
import {
EasNonInteractiveAndJsonFlags,
resolveNonInteractiveAndJsonFlags,
} from '../../../commandUtils/flags';
import {
EmbeddedUpdateMutation,
isEmbeddedUpdateNotFoundError,
} from '../../../graphql/mutations/EmbeddedUpdateMutation';
import Log from '../../../log';
import { confirmAsync } from '../../../prompts';
import { enableJsonOutput, printJsonOnlyOutput } from '../../../utils/json';

export default class UpdateEmbeddedDelete extends EasCommand {
static override description = 'delete an embedded update registered with EAS Update';

static override args = {
id: Args.string({
required: true,
description: 'The ID of the embedded update (manifest UUID from app.manifest).',
}),
};

static override flags = {
...EasNonInteractiveAndJsonFlags,
};

static override contextDefinition = {
...this.ContextOptions.LoggedIn,
};

async runAsync(): Promise<void> {
const {
args: { id: embeddedUpdateId },
flags,
} = await this.parse(UpdateEmbeddedDelete);
const { json: jsonFlag, nonInteractive } = resolveNonInteractiveAndJsonFlags(flags);

const {
loggedIn: { graphqlClient },
} = await this.getContextAsync(UpdateEmbeddedDelete, { nonInteractive });

if (jsonFlag) {
enableJsonOutput();
}

if (!nonInteractive) {
const shouldContinue = await confirmAsync({
message:
`${chalk.yellow(`This will delete embedded update "${embeddedUpdateId}".`)} ${chalk.red(
'This is permanent.'
)}\n\n` +
`The next build that ships with this manifest UUID will no longer be eligible for ` +
`embedded-bundle bsdiff patches until you re-upload.\n\n` +
`Continue?`,
});
if (!shouldContinue) {
Log.log('Aborted.');
return;
}
}

try {
await EmbeddedUpdateMutation.deleteEmbeddedUpdateAsync(graphqlClient, {
id: embeddedUpdateId,
});
} catch (e: unknown) {
if (isEmbeddedUpdateNotFoundError(e)) {
Errors.error(
`No embedded update found with id "${embeddedUpdateId}" for this project.`,
{ exit: 1 }
);
}
throw e;
}

if (jsonFlag) {
printJsonOnlyOutput({ id: embeddedUpdateId });
return;
}
Log.withTick(`Deleted embedded update ${embeddedUpdateId}`);
}
}
38 changes: 38 additions & 0 deletions packages/eas-cli/src/graphql/mutations/EmbeddedUpdateMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ export function isEmbeddedUpdateAlreadyExistsError(error: unknown): boolean {
);
}

export function isEmbeddedUpdateNotFoundError(error: unknown): boolean {
return (
error instanceof CombinedError &&
error.graphQLErrors.some(e => e.extensions?.['errorCode'] === 'NOT_FOUND_ERROR')
);
}

// TODO: replace with generated types once expo/universe#27767 lands and codegen runs.
type DeleteEmbeddedUpdateMutationVariables = { id: string };
type DeleteEmbeddedUpdateMutation = {
embeddedUpdate: { deleteEmbeddedUpdate: { id: string } };
};

export const EmbeddedUpdateMutation = {
async uploadEmbeddedUpdateAsync(
graphqlClient: ExpoGraphqlClient,
Expand Down Expand Up @@ -61,4 +74,29 @@ export const EmbeddedUpdateMutation = {
);
return data.embeddedUpdate.uploadEmbeddedUpdate;
},

async deleteEmbeddedUpdateAsync(
graphqlClient: ExpoGraphqlClient,
{ id }: { id: string }
): Promise<{ id: string }> {
const data = await withErrorHandlingAsync(
graphqlClient
.mutation<DeleteEmbeddedUpdateMutation, DeleteEmbeddedUpdateMutationVariables>(
/* eslint-disable graphql/template-strings */
gql`
mutation DeleteEmbeddedUpdate($id: ID!) {
embeddedUpdate {
deleteEmbeddedUpdate(id: $id) {
id
}
}
}
`,
/* eslint-enable graphql/template-strings */
{ id }
)
.toPromise()
);
return data.embeddedUpdate.deleteEmbeddedUpdate;
},
};
Loading