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 { ORG_OPT, orgOptsSchema, output, promptableOption, REASONABLE_NAME_SCHEMA, requireSession, safelyPromptTextWithValidation, sessionIntro, useOrganization, ORG_OPTION_DESCRIPTIONS, } from "../utils" import { accent, dimmed } from "../lib/io" import { clearSpinner, startSpinner } from "../lib/spinner" /** Arguments accepted by the organization rename command. */ const renameOrgOptsSchema = orgOptsSchema.extend({ name: promptableOption(REASONABLE_NAME_SCHEMA, "--name"), }) export const orgRenameCommand = defineCommand({ name: "rename", description: "Rename an organization", options: { ...globalOptions, ...ORG_OPT, name: { type: "string", short: "n", }, }, optionDescriptions: { ...ORG_OPTION_DESCRIPTIONS, name: "New name for the organization", }, schema: globalOptionsSchema.extend(renameOrgOptsSchema.shape), run: renameOrganization, }) /** * Renames an organization without changing its internal authentication slug. * * @param opts - Validated organization selector and optional new name. */ export async function renameOrganization( opts: z.infer, ) { const session = await requireSession("rename an organization") output.normal(() => sessionIntro(session)) const org = await useOrganization(opts) const newName = opts.name ?? (await promptName(org.name)) startSpinner("Renaming organization") await apiClient.org.update({ organizationId: org.id, name: newName, }) clearSpinner() output .normal(() => log.success(`Renamed ${dimmed(org.name)} to ${accent(newName)}`), ) .json({ ...org, name: newName }) } /** * Prompts for and validates a replacement organization name. * * @param currentName - Current organization name used as the prompt default. */ async function promptName(currentName: string) { return await safelyPromptTextWithValidation( { message: "What should the new organization name be?", placeholder: currentName, defaultValue: currentName, }, REASONABLE_NAME_SCHEMA, "--name", ) }