-
Notifications
You must be signed in to change notification settings - Fork 85
/
delete.ts
148 lines (135 loc) · 4.46 KB
/
delete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Flags } from '@oclif/core';
import gql from 'graphql-tag';
import EasCommand from '../../commandUtils/EasCommand';
import { ensureBuildExistsAsync, fetchBuildsAsync, formatBuild } from '../../commandUtils/builds';
import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient';
import { EASNonInteractiveFlag } from '../../commandUtils/flags';
import { withErrorHandlingAsync } from '../../graphql/client';
import { Build, DeleteBuildMutation, DeleteBuildMutationVariables } from '../../graphql/generated';
import Log from '../../log';
import { ora } from '../../ora';
import { RequestedPlatform } from '../../platform';
import { getDisplayNameForProjectIdAsync } from '../../project/projectUtils';
import { confirmAsync, selectAsync } from '../../prompts';
async function deleteBuildAsync(
graphqlClient: ExpoGraphqlClient,
buildId: string
): Promise<Pick<Build, 'id'>> {
const data = await withErrorHandlingAsync(
graphqlClient
.mutation<DeleteBuildMutation, DeleteBuildMutationVariables>(
gql`
mutation DeleteBuildMutation($buildId: ID!) {
build(buildId: $buildId) {
deleteBuild(buildId: $buildId) {
id
}
}
}
`,
{ buildId }
)
.toPromise()
);
return data.build.deleteBuild;
}
export async function selectBuildToDeleteAsync(
graphqlClient: ExpoGraphqlClient,
projectId: string,
projectDisplayName: string,
filters?: {
platform?: RequestedPlatform;
profile?: string;
}
): Promise<string | null> {
const spinner = ora().start('Fetching builds…');
let builds;
try {
builds = await fetchBuildsAsync({ graphqlClient, projectId, filters });
spinner.stop();
} catch (error) {
spinner.fail(
`Something went wrong and we couldn't fetch the builds for the project ${projectDisplayName}.`
);
throw error;
}
if (builds.length === 0) {
Log.warn(`We couldn't find any builds for the project ${projectDisplayName}.`);
return null;
} else {
const buildId = await selectAsync<string>(
'Which build do you want to delete?',
builds.map(build => ({
title: formatBuild(build),
value: build.id,
}))
);
return (await confirmAsync({
message: 'Are you sure you want to delete it?',
}))
? buildId
: null;
}
}
export default class BuildDelete extends EasCommand {
static override description = 'delete a build';
static override args = [{ name: 'BUILD_ID' }];
static override flags = {
...EASNonInteractiveFlag,
platform: Flags.enum({
char: 'p',
description: 'Filter builds by the platform if build ID is not provided',
options: Object.values(RequestedPlatform),
}),
profile: Flags.string({
char: 'e',
description: 'Filter builds by build profile if build ID is not provided',
helpValue: 'PROFILE_NAME',
}),
};
static override contextDefinition = {
...this.ContextOptions.ProjectId,
...this.ContextOptions.LoggedIn,
...this.ContextOptions.Vcs,
};
async runAsync(): Promise<void> {
const {
args: { BUILD_ID: buildIdFromArg },
flags: { 'non-interactive': nonInteractive, platform, profile },
} = await this.parse(BuildDelete);
if (buildIdFromArg && (platform || profile)) {
throw new Error(
'Build ID cannot be used together with platform and profile flags. They are used to filter the list of builds when not providing the build ID'
);
}
const {
projectId,
loggedIn: { graphqlClient },
} = await this.getContextAsync(BuildDelete, { nonInteractive });
const displayName = await getDisplayNameForProjectIdAsync(graphqlClient, projectId);
if (buildIdFromArg) {
await ensureBuildExistsAsync(graphqlClient, buildIdFromArg);
}
let buildId: string | null = buildIdFromArg;
if (!buildId) {
if (nonInteractive) {
throw new Error('Build ID must be provided in non-interactive mode');
}
buildId = await selectBuildToDeleteAsync(graphqlClient, projectId, displayName, {
platform,
profile,
});
if (!buildId) {
return;
}
}
const spinner = ora().start('Deleting the build...');
try {
await deleteBuildAsync(graphqlClient, buildId);
spinner.succeed('Build deleted');
} catch (error) {
spinner.fail(`Something went wrong and we couldn't delete your build ${buildId}`);
throw error;
}
}
}