import { confirm, log } from "@clack/prompts" import { defineCommand } from "../lib/command" import { globalOptions, globalOptionsSchema } from "../lib/global-options" import z from "zod" import { apiClient } from "../lib/api-client" import { output, PROJECT_OPT, projectOptsSchema, safelyPrompt, sessionIntro, useProject, requireSession, PROJECT_OPTION_DESCRIPTIONS, } from "../utils" import { accent, dimmed } from "../lib/io" import { clearSpinner, startSpinner } from "../lib/spinner" /** Arguments accepted by the project deletion command. */ const deleteProjectOptsSchema = projectOptsSchema.extend({ yes: z.boolean().default(false), }) export const projectDeleteCommand = defineCommand({ name: "delete", description: "Delete a project", options: { ...globalOptions, ...PROJECT_OPT, yes: { type: "boolean", short: "y", default: false, }, }, optionDescriptions: { ...PROJECT_OPTION_DESCRIPTIONS, yes: "Skip confirmation prompt", }, schema: globalOptionsSchema.extend(deleteProjectOptsSchema.shape), run: deleteProject, }) /** * Deletes a project after confirmation. * * @param opts - Validated project selector and confirmation options. */ export async function deleteProject( opts: z.infer, ) { const session = await requireSession() output.normal(() => sessionIntro(session)) const project = await useProject(opts, { mode: "select", }) if (!opts.yes) { if ( !(await safelyPrompt( () => confirm({ message: `Are you sure you want to delete ${accent(project.name)}? This cannot be undone.`, initialValue: false, }), "--yes", )) ) { output.json({ cancelled: true }) return } } startSpinner("Deleting project") await apiClient.project.delete({ projectId: project.id }) clearSpinner() output .normal(() => log.success(`Deleted ${accent(project.name)} ${dimmed(project.id)}`), ) .json({ deleted: true, project }) }