|
| 1 | +import { Command } from "../command"; |
| 2 | +import { Options } from "../options"; |
| 3 | +import { needProjectId } from "../projectUtils"; |
| 4 | +import { FirebaseError, getError } from "../error"; |
| 5 | +import { promptOnce } from "../prompt"; |
| 6 | +import * as utils from "../utils"; |
| 7 | +import * as apphosting from "../gcp/apphosting"; |
| 8 | +import { printBackendsTable } from "./apphosting-backends-list"; |
| 9 | +import { |
| 10 | + deleteBackendAndPoll, |
| 11 | + getBackendForAmbiguousLocation, |
| 12 | + getBackendForLocation, |
| 13 | +} from "../apphosting/backend"; |
| 14 | +import * as ora from "ora"; |
| 15 | + |
| 16 | +export const command = new Command("apphosting:backends:delete <backend>") |
| 17 | + .description("delete a Firebase App Hosting backend") |
| 18 | + .option("-l, --location <location>", "specify the location of the backend") |
| 19 | + .withForce() |
| 20 | + .before(apphosting.ensureApiEnabled) |
| 21 | + .action(async (backendId: string, options: Options) => { |
| 22 | + const projectId = needProjectId(options); |
| 23 | + if (options.location !== undefined) { |
| 24 | + utils.logWarning( |
| 25 | + "--location is being removed in the next major release. " + |
| 26 | + "Instead, the CLI will list all possible backends to be deleted.", |
| 27 | + ); |
| 28 | + } |
| 29 | + let location = (options.location as string) ?? "-"; |
| 30 | + let backend: apphosting.Backend; |
| 31 | + if (location === "-" || location === "") { |
| 32 | + backend = await getBackendForAmbiguousLocation( |
| 33 | + projectId, |
| 34 | + backendId, |
| 35 | + "Please select the location of the backend you'd like to delete:", |
| 36 | + ); |
| 37 | + location = apphosting.parseBackendName(backend.name).location; |
| 38 | + } else { |
| 39 | + backend = await getBackendForLocation(projectId, location, backendId); |
| 40 | + } |
| 41 | + |
| 42 | + utils.logWarning("You are about to permanently delete this backend:"); |
| 43 | + printBackendsTable([backend]); |
| 44 | + |
| 45 | + const confirmDeletion = await promptOnce( |
| 46 | + { |
| 47 | + type: "confirm", |
| 48 | + name: "force", |
| 49 | + default: false, |
| 50 | + message: "Are you sure?", |
| 51 | + }, |
| 52 | + options, |
| 53 | + ); |
| 54 | + if (!confirmDeletion) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const spinner = ora("Deleting backend...").start(); |
| 59 | + try { |
| 60 | + await deleteBackendAndPoll(projectId, location, backendId); |
| 61 | + spinner.succeed(`Successfully deleted the backend: ${backendId}`); |
| 62 | + } catch (err: unknown) { |
| 63 | + spinner.stop(); |
| 64 | + throw new FirebaseError(`Failed to delete backend: ${backendId}.`, { |
| 65 | + original: getError(err), |
| 66 | + }); |
| 67 | + } |
| 68 | + }); |
0 commit comments