-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { getConfig } from '@expo/config'; | ||
import { Command } from '@oclif/command'; | ||
import assert from 'assert'; | ||
import chalk from 'chalk'; | ||
import nullthrows from 'nullthrows'; | ||
import ora from 'ora'; | ||
|
||
import { WebhookFragment } from '../../graphql/generated'; | ||
import { WebhookMutation } from '../../graphql/mutations/WebhookMutation'; | ||
import { WebhookQuery } from '../../graphql/queries/WebhookQuery'; | ||
import Log from '../../log'; | ||
import { findProjectRootAsync, getProjectIdAsync } from '../../project/projectUtils'; | ||
import { promptAsync, toggleConfirmAsync } from '../../prompts'; | ||
import { ensureLoggedInAsync } from '../../user/actions'; | ||
import { formatWebhook } from '../../webhooks/formatWebhook'; | ||
|
||
export default class WebhookDelete extends Command { | ||
static description = 'Delete a webhook on the current project.'; | ||
|
||
static args = [ | ||
{ | ||
name: 'ID', | ||
required: false, | ||
description: 'ID of the webhook to delete', | ||
}, | ||
]; | ||
|
||
async run() { | ||
await ensureLoggedInAsync(); | ||
let { | ||
args: { ID: webhookId }, | ||
} = this.parse(WebhookDelete); | ||
|
||
const projectDir = await findProjectRootAsync(process.cwd()); | ||
if (!projectDir) { | ||
throw new Error('Please run this command inside a project directory.'); | ||
} | ||
const { exp } = getConfig(projectDir, { skipSDKVersionRequirement: true }); | ||
const projectId = await getProjectIdAsync(exp); | ||
|
||
let webhook: WebhookFragment | undefined = | ||
webhookId && (await WebhookQuery.byIdAsync(webhookId)); | ||
if (!webhookId) { | ||
const webhooks = await fetchWebhooksByAppId(projectId); | ||
if (webhooks.length === 0) { | ||
process.exit(1); | ||
} | ||
({ 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(); | ||
Log.warn(`You are about to permamently 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(webhookId); | ||
spinner.succeed('Successfully deleted the webhook'); | ||
} catch (err) { | ||
spinner.fail('Failed to delete the webhook'); | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
async function fetchWebhooksByAppId(appId: string): Promise<WebhookFragment[]> { | ||
const spinner = ora('Fetching webhooks').start(); | ||
try { | ||
const webhooks = await WebhookQuery.byAppIdAsync(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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters