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 { ORG_OPT, orgOptsSchema, output, safelyPrompt, sessionIntro, useOrganization, requireSession, ORG_OPTION_DESCRIPTIONS, } from "../utils" import { accent } from "../lib/io" import { clearSpinner, startSpinner } from "../lib/spinner" /** Arguments accepted by the organization leave command. */ const leaveOrgOptsSchema = orgOptsSchema.extend({ yes: z.boolean().default(false), }) export const orgLeaveCommand = defineCommand({ name: "leave", description: "Leave an organization", options: { ...globalOptions, ...ORG_OPT, yes: { type: "boolean", short: "y", default: false, }, }, optionDescriptions: { ...ORG_OPTION_DESCRIPTIONS, yes: "Skip confirmation prompt", }, schema: globalOptionsSchema.extend(leaveOrgOptsSchema.shape), run: leaveOrganization, }) /** * Removes the current user from the resolved organization. * * @param opts - Validated organization selector and confirmation options. */ export async function leaveOrganization( opts: z.infer, ) { const session = await requireSession() output.normal(() => sessionIntro(session)) const org = await useOrganization(opts) if (!opts.yes) { if ( !(await safelyPrompt( () => confirm({ message: `Are you sure you want to leave ${accent(org.name)}?`, initialValue: false, }), "--yes", )) ) { output.json({ cancelled: true }) return } } startSpinner("Leaving organization") await apiClient.org.leave({ organizationId: org.id }) clearSpinner() output .normal(() => log.success(`Left ${accent(org.name)}`)) .json({ left: true, organization: org, }) }