import chalk from 'chalk'; import type Client from '../../util/client'; import { requireProjectContext } from '../../util/projects/require-project-context'; import output from '../../output-manager'; import { deleteSubcommand } from './command'; import { parseSubcommandArgs, resolveRoutes, offerAutoPromote, withGlobalFlags, } from './shared'; import { outputAgentError } from '../../util/agent-output'; import { AGENT_STATUS, AGENT_REASON } from '../../util/agent-output-constants'; import getRoutes from '../../util/routes/get-routes'; import getRouteVersions from '../../util/routes/get-route-versions'; import deleteRoutes from '../../util/routes/delete-routes'; import stamp from '../../util/output/stamp'; import { getRouteTypeLabel } from '../../util/routes/types'; export default async function deleteRoute(client: Client, argv: string[]) { const parsed = await parseSubcommandArgs(argv, deleteSubcommand, client); if (typeof parsed === 'number') return parsed; const link = await requireProjectContext( client, 'routes', parsed.flags['--project'] ); if (typeof link === 'number') return link; const { project, org } = link; const teamId = org.type === 'team' ? org.id : undefined; const { args, flags } = parsed; const skipConfirmation = flags['--yes'] as boolean | undefined; if (args.length === 0) { if (client.nonInteractive) { outputAgentError( client, { status: AGENT_STATUS.ERROR, reason: AGENT_REASON.MISSING_ARGUMENTS, message: 'At least one route name or ID is required.', next: [ { command: withGlobalFlags( client, 'routes remove --yes' ), when: 'pass one or more ids/names and --yes to skip confirm', }, ], }, 2 ); } output.error( `At least one route name or ID is required. Usage: ${withGlobalFlags(client, 'routes remove [...]')}` ); return 2; } // Check for existing staging version (for auto-promote logic) const { versions } = await getRouteVersions(client, project.id, { teamId }); const existingStagingVersion = versions.find(v => v.isStaging); // Fetch all routes to resolve identifiers output.spinner('Fetching routes'); const { routes } = await getRoutes(client, project.id, { teamId }); output.stopSpinner(); if (routes.length === 0) { if (client.nonInteractive) { outputAgentError( client, { status: AGENT_STATUS.ERROR, reason: AGENT_REASON.NOT_FOUND, message: 'No routes found in this project.', }, 1 ); } output.error('No routes found in this project.'); return 1; } // Resolve each identifier to a route const resolved = await resolveRoutes(client, routes, args); if (!resolved) return 1; // Show what will be removed output.print('\n'); output.log( `The following ${resolved.length === 1 ? 'route' : `${resolved.length} routes`} will be removed:` ); for (const route of resolved) { const typeLabels = getRouteTypeLabel(route); output.print( ` ${chalk.red('×')} ${route.name} ${chalk.gray(`(${route.route.src})`)} ${chalk.gray(`[${typeLabels}]`)}\n` ); } output.print('\n'); // Confirm if (!skipConfirmation) { if (client.nonInteractive) { outputAgentError( client, { status: AGENT_STATUS.ERROR, reason: AGENT_REASON.CONFIRMATION_REQUIRED, message: 'Removing routes requires confirmation. Pass --yes to skip the prompt in non-interactive mode.', next: [ { command: withGlobalFlags( client, `routes remove ${args.join(' ')} --yes` ), when: 're-run with --yes to confirm without prompt', }, ], }, 1 ); } const confirmed = await client.input.confirm( `Remove ${resolved.length === 1 ? 'this route' : `these ${resolved.length} routes`}?`, false ); if (!confirmed) { output.log('Aborted.'); return 0; } } // Remove const deleteStamp = stamp(); output.spinner( `Removing ${resolved.length === 1 ? 'route' : `${resolved.length} routes`}` ); try { const { deletedCount, version } = await deleteRoutes( client, project.id, resolved.map(r => r.id), { teamId } ); output.log( `${chalk.cyan('Removed')} ${deletedCount} ${deletedCount === 1 ? 'route' : 'routes'} ${chalk.gray(deleteStamp())}` ); // Auto-promote offer await offerAutoPromote( client, project.id, version, !!existingStagingVersion, { teamId, skipPrompts: skipConfirmation } ); return 0; } catch (e: unknown) { const error = e as { message?: string }; if (client.nonInteractive) { outputAgentError( client, { status: AGENT_STATUS.ERROR, reason: AGENT_REASON.API_ERROR, message: 'Failed to remove routes for this project. See hint for error details.', hint: error.message || 'Retry with the same command once transient issues are resolved.', }, 1 ); } output.error(error.message || 'Failed to remove routes'); return 1; } }