-
Notifications
You must be signed in to change notification settings - Fork 76
/
delete.ts
119 lines (105 loc) · 3.59 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
import assert from 'assert';
import chalk from 'chalk';
import nullthrows from 'nullthrows';
import EasCommand from '../../commandUtils/EasCommand';
import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient';
import { EASNonInteractiveFlag } from '../../commandUtils/flags';
import { WebhookFragment } from '../../graphql/generated';
import { WebhookMutation } from '../../graphql/mutations/WebhookMutation';
import { WebhookQuery } from '../../graphql/queries/WebhookQuery';
import Log from '../../log';
import { ora } from '../../ora';
import { promptAsync, toggleConfirmAsync } from '../../prompts';
import { formatWebhook } from '../../webhooks/formatWebhook';
export default class WebhookDelete extends EasCommand {
static override description = 'delete a webhook';
static override args = [
{
name: 'ID',
required: false,
description: 'ID of the webhook to delete',
},
];
static override flags = {
...EASNonInteractiveFlag,
};
static override contextDefinition = {
...this.ContextOptions.ProjectConfig,
...this.ContextOptions.LoggedIn,
};
async runAsync(): Promise<void> {
let {
args: { ID: webhookId },
flags: { 'non-interactive': nonInteractive },
} = await this.parse(WebhookDelete);
const {
privateProjectConfig: { projectId },
loggedIn: { graphqlClient },
} = await this.getContextAsync(WebhookDelete, {
nonInteractive,
});
let webhook: WebhookFragment | undefined =
webhookId && (await WebhookQuery.byIdAsync(graphqlClient, webhookId));
if (!webhookId) {
const webhooks = await fetchWebhooksByAppIdAsync(graphqlClient, projectId);
if (webhooks.length === 0) {
process.exit(1);
}
if (nonInteractive) {
throw new Error('Must supply ID argument in non-interactive mode');
}
({ webhook } = await promptAsync({
type: 'autocomplete',
name: 'webhook',
message: 'Pick the webhook to be deleted:',
choices: webhooks.map(i => ({
title: `${chalk.bold(i.url)} (Event: ${i.event}, ID: ${i.id})`,
value: i,
})),
}));
webhookId = nullthrows(webhook).id;
}
assert(webhook, 'Webhook must be defined here');
Log.addNewLineIfNone();
Log.log(formatWebhook(webhook));
Log.newLine();
if (!nonInteractive) {
Log.warn(`You are about to permanently delete this webhook.\nThis action is irreversible.`);
Log.newLine();
const confirmed = await toggleConfirmAsync({
message: 'Are you sure you wish to proceed?',
});
if (!confirmed) {
Log.error(`Canceled deletion of the webhook`);
process.exit(1);
}
}
const spinner = ora('Deleting the webhook').start();
try {
await WebhookMutation.deleteWebhookAsync(graphqlClient, webhookId);
spinner.succeed('Successfully deleted the webhook');
} catch (err) {
spinner.fail('Failed to delete the webhook');
throw err;
}
}
}
async function fetchWebhooksByAppIdAsync(
graphqlClient: ExpoGraphqlClient,
appId: string
): Promise<WebhookFragment[]> {
const spinner = ora('Fetching webhooks').start();
try {
const webhooks = await WebhookQuery.byAppIdAsync(graphqlClient, appId);
if (webhooks.length === 0) {
spinner.fail('There are no webhooks on the project');
return [];
} else {
spinner.succeed(`Successfully fetched ${webhooks.length} webhooks`);
return webhooks;
}
} catch (err) {
spinner.fail("Something went wrong and we couldn't fetch the webhook list");
throw err;
}
}