import { 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, promptableOption, REASONABLE_NAME_SCHEMA, safelyPromptTextWithValidation, 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 rename command. */ const renameProjectOptsSchema = projectOptsSchema.extend({ name: promptableOption(REASONABLE_NAME_SCHEMA, "--name"), }) export const projectRenameCommand = defineCommand({ name: "rename", description: "Rename a project", options: { ...globalOptions, ...PROJECT_OPT, name: { type: "string", short: "n", }, }, optionDescriptions: { ...PROJECT_OPTION_DESCRIPTIONS, name: "New name for the project", }, schema: globalOptionsSchema.extend(renameProjectOptsSchema.shape), run: renameProject, }) /** * Renames a project. * * @param opts - Validated project selector and optional new name. */ export async function renameProject( opts: z.infer, ) { const session = await requireSession() output.normal(() => sessionIntro(session)) const project = await useProject(opts, { mode: "select", }) const newName = opts.name ?? (await promptName(project.name)) startSpinner("Renaming project") await apiClient.project.update({ projectId: project.id, name: newName, }) clearSpinner() output .normal(() => log.success(`Renamed to ${accent(newName)} ${dimmed(project.id)}`), ) .json({ ...project, name: newName }) } /** * Prompts for and validates a replacement project name. * * @param currentName - Current project name used as the prompt default. */ async function promptName(currentName: string) { return await safelyPromptTextWithValidation( { message: "What should the new project name be?", placeholder: currentName, initialValue: currentName, }, REASONABLE_NAME_SCHEMA, "--name", ) }