import { createImplementation } from "@webiny/di"; import { CliCommandFactory } from "@webiny/cli-core/abstractions/index.js"; import { StdioService } from "@webiny/cli-core/abstractions/index.js"; import { UiService } from "@webiny/cli-core/abstractions/index.js"; import { measureDuration } from "@webiny/cli-core/features/utils/index.js"; import { createBaseAppOptions } from "@webiny/cli-core/features/common/index.js"; import { AppName } from "@webiny/project"; import { GetAwsProjectSdkService } from "~/abstractions/GetAwsProjectSdkService.js"; export interface IDestroyNoAppParams { variant?: string; region?: string; env: string; } export interface IDestroyWithAppParams extends IDestroyNoAppParams { app: AppName; build?: boolean; preview?: boolean; } export type IDestroyCommandParams = IDestroyNoAppParams | IDestroyWithAppParams; export class DestroyCommand implements CliCommandFactory.Interface { constructor( private getAwsProjectSdkService: GetAwsProjectSdkService.Interface, private uiService: UiService.Interface, private stdioService: StdioService.Interface ) {} async execute(): Promise> { const projectSdk = await this.getAwsProjectSdkService.execute(); return { name: "destroy", description: "Destroys specified app or all apps in the project", examples: [ "$0 destroy api --env dev", "$0 destroy admin --env prod", "$0 destroy --env prod", "$0 destroy" ], params: [ { name: "app", description: "Name of the app (core, admin, or api)", type: "string" } ], options: createBaseAppOptions(projectSdk), handler: async (params: IDestroyCommandParams) => { if ("app" in params) { return this.destroyApp(params); } await this.destroyApp({ ...params, app: "admin" }); await this.destroyApp({ ...params, app: "api" }); await this.destroyApp({ ...params, app: "core" }); } }; } private async destroyApp(params: IDestroyWithAppParams) { const projectSdk = await this.getAwsProjectSdkService.execute(); const ui = this.uiService; const stdio = this.stdioService; const { pulumiProcess } = await projectSdk.destroyApp(params); const getDestroyDuration = measureDuration(); try { ui.info(`Destroying %s app...`, params.app); ui.emptyLine(); pulumiProcess.stdout!.pipe(stdio.getStdout()); pulumiProcess.stderr!.pipe(stdio.getStderr()); await pulumiProcess; ui.success(`Destroyed in ${getDestroyDuration()}.`); } catch (e) { // Imported here so that loading the destroy command doesn't load the Pulumi SDK. By // the time a destroy fails, the SDK is loaded anyway, since it started the process. const { PulumiError } = await import("@webiny/pulumi-sdk"); if (e instanceof PulumiError) { ui.error("Destroy failed, please check the details above."); } else { ui.text(e.message); ui.error("Destroy failed, please check the details above."); } throw e; } } } export const destroyCommand = createImplementation({ abstraction: CliCommandFactory, implementation: DestroyCommand, dependencies: [GetAwsProjectSdkService, UiService, StdioService] });