import { confirm, isCancel } from "@clack/prompts"; import { Command } from "commander"; import z from "zod"; import { createClient } from "../../../utils/client"; import { addGlobalOptions, extendGlobalOptions, parseOptions, } from "../../../utils/global-options"; import { log, spinner } from "../../../utils/logger"; import { resolveStore } from "../../../utils/store"; const DeleteFileSchema = extendGlobalOptions({ nameOrId: z.string().min(1, { error: '"name-or-id" is required' }), fileId: z.string().min(1, { error: '"file-id" is required' }), yes: z.boolean().optional(), }); export function createDeleteCommand(): Command { const deleteCommand = addGlobalOptions( new Command("delete") .alias("rm") .description("Delete a file from store") .argument("", "Name or ID of the store") .argument("", "ID of the file") .option("-y, --yes", "Skip confirmation prompt") ); deleteCommand.action(async (nameOrId: string, fileId: string) => { const deleteSpinner = spinner(); try { const mergedOptions = deleteCommand.optsWithGlobals(); const parsedOptions = parseOptions(DeleteFileSchema, { ...mergedOptions, nameOrId, fileId, }); const client = createClient(parsedOptions); const store = await resolveStore(client, parsedOptions.nameOrId); // Confirmation prompt unless --yes is used if (!parsedOptions.yes) { const confirmed = await confirm({ message: `Are you sure you want to delete file "${parsedOptions.fileId}" from store "${store.name}" (${store.id})? This action cannot be undone.`, initialValue: false, }); if (isCancel(confirmed) || !confirmed) { log.warn("Deletion cancelled."); return; } } deleteSpinner.start("Deleting file..."); await client.stores.files.delete(parsedOptions.fileId, { store_identifier: store.id, }); deleteSpinner.stop(`File ${parsedOptions.fileId} deleted successfully`); } catch (error) { deleteSpinner.stop(); log.error( error instanceof Error ? error.message : "Failed to delete file" ); process.exit(1); } }); return deleteCommand; }