import { log } from "@clack/prompts" import { defineCommand } from "../lib/command" import { globalOptions, globalOptionsSchema } from "../lib/global-options" import { z } from "zod" import { deploymentSessionIntro, openDeploymentAuthorization, pollDeployment, } from "../lib/deployment-lifecycle" import { accent } from "../lib/io" import { clearSpinner, startSpinner } from "../lib/spinner" import { output } from "../utils" /** Options accepted by the deployment authorization command. */ const authorizeDeploymentOptsSchema = globalOptionsSchema.extend({ noOpen: z.boolean().default(false), positionals: z.tuple([z.string().min(1)]), }) export const deploymentAuthorizeCommand = defineCommand({ name: "authorize", description: "Complete and wait for project setup", options: { ...globalOptions, noOpen: { type: "boolean", default: false, }, }, optionDescriptions: { noOpen: "Print the setup URL without opening a browser", }, positionals: [ { name: "deploymentId", description: "Deployment ID", required: true, }, ], schema: globalOptionsSchema.extend(authorizeDeploymentOptsSchema.shape), run: authorizeDeployment, }) /** * Resumes a deployment, handing off secret entry to the focused web flow. * * @param opts - Deployment identity and browser-opening preference. */ export async function authorizeDeployment( opts: z.infer, ) { await deploymentSessionIntro() const [deploymentId] = opts.positionals startSpinner( "Checking deployment", `Stopped waiting. Deployment ${deploymentId} will continue in the background.`, ) let deployment = await pollDeployment(deploymentId, { returnForAuthorization: true, }) clearSpinner() if (deployment.status === "awaiting_authorization") { await openDeploymentAuthorization(deployment, { openBrowser: !opts.noOpen, }) startSpinner( "Waiting for project setup", `Stopped waiting. Deployment ${deploymentId} remains pending setup.`, ) deployment = await pollDeployment(deploymentId, { returnForAuthorization: false, }) clearSpinner() } output .normal(() => log.success(`Deployment ${accent(deployment.id)} succeeded`)) .json(deployment) }