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, requireSession, safelyPrompt, safelyPromptTextWithValidation, sessionIntro, useOrganization, ORG_OPTION_DESCRIPTIONS, } from "../utils" import { accent } from "../lib/io" import { clearSpinner, startSpinner } from "../lib/spinner" /** Arguments accepted by the organization member removal command. */ const removeOrgOptsSchema = orgOptsSchema.extend({ positionals: z.tuple([z.string().min(1).optional()]), yes: z.boolean().default(false), }) export const orgRemoveCommand = defineCommand({ name: "remove", description: "Remove a member from an organization", options: { ...globalOptions, yes: { type: "boolean", short: "y", default: false, }, ...ORG_OPT, }, optionDescriptions: { yes: "Skip confirmation prompt", ...ORG_OPTION_DESCRIPTIONS, }, positionals: [ { name: "member", description: "Email or member ID to remove", required: false, }, ], schema: globalOptionsSchema.extend(removeOrgOptsSchema.shape), run: removeMember, }) /** * Removes a member from the resolved organization. * * @param opts - Validated organization, member, and confirmation options. */ export async function removeMember(opts: z.infer) { const session = await requireSession("remove organization members") output.normal(() => sessionIntro(session)) const org = await useOrganization(opts) const member = opts.positionals[0] ?? (await safelyPromptTextWithValidation( { message: "Which member should we remove?", }, z.string().min(1), "member", )) if (!opts.yes) { if ( !(await safelyPrompt( () => confirm({ message: `Remove ${accent(member)} from ${accent(org.name)}?`, initialValue: false, }), "--yes", )) ) { output.json({ cancelled: true }) return } } startSpinner(`Removing ${accent(member)}`) await apiClient.org.remove({ memberIdOrEmail: member, organizationId: org.id, }) clearSpinner() output .normal(() => log.success(`Removed ${accent(member)} from ${accent(org.name)}`), ) .json({ removed: true, member, organization: org }) }