import{o as e}from"./chunk-Bdh3yLIe.js";import{n as t}from"./source-DkXWTsmk.js";import{t as n}from"./execa-BRFiuxTP.js";import{t as r}from"./lib-BqQgGqqb.js";import{a as i,c as a,d as o,l as s,n as c,u as l}from"./utils-l-LeUg2v.js";import{n as u,t as d}from"./globalOptions-BzW3wHo-.js";import{r as f,t as p}from"./state-NQaDy_hK.js";import{t as m}from"./parseSettings-DJgpSIYp.js";import{t as h}from"./getUserPkgManager-BqdkpOAa.js";import g from"node:path";var _=e(r(),1);const v=/^[✔\s]+/,y=/\s{2,}/,b=/^v/;async function x(){try{return await n(`vercel`,[`--version`]),!0}catch{return!1}}async function S(){let e=h(),r=s();r.start(`Installing Vercel CLI...`);try{return await n(e,[e===`npm`?`install`:`add`,`-g`,`vercel`]),r.stop(`Vercel CLI installed successfully`),!0}catch(e){return r.stop(`Failed to install Vercel CLI`),console.error(t.red(`Error installing Vercel CLI:`),e),!1}}async function C(){try{return!!(await _.default.readJSON(`.vercel/project.json`)).projectId}catch{return f.debug&&console.log(`
Debug: No Vercel project configuration found`),!1}}async function w(){try{f.debug&&console.log(`
Debug: Running vercel teams list command...`);let e=await n(`vercel`,[`teams`,`list`],{all:!0});f.debug&&console.log(`
Debug: Command output:`,e.all);let t=(e.all??``).split(`
`).filter(Boolean),r=t.findIndex(e=>e.includes(`id`));if(r===-1)return[];let i=t.slice(r+1);if(f.debug){console.log(`
Debug: Team lines:`);for(let e of i)console.log(`"${e}"`)}let a=i.map(e=>{let[t,...n]=e.replace(v,``).trim().split(y);return!t||n.length===0?null:{slug:t,name:n.join(` `).trim()}}).filter(e=>e!==null);return f.debug&&console.log(`
Debug: Parsed teams:`,a),a}catch(e){return f.debug&&console.error(`Error getting Vercel teams:`,e),[]}}async function T(){let e=s();try{let r=(await _.default.readJSON(`package.json`)).name,o=await w(),s=``;if(o.length>1){let e=await a({message:`Select a team to deploy under:`,options:[...o.map(e=>({value:e.slug,label:e.name}))]});if(i(e))return console.log(t.yellow(`
Operation cancelled`)),!1;e&&typeof e==`string`&&(s=`--scope=${e}`)}return e.start(`Creating Vercel project...`),await n(`vercel`,[`link`,`--yes`,...s?[s]:[]],{env:{VERCEL_PROJECT_NAME:r}}),e.message(`Pulling project settings...`),await n(`vercel`,[`pull`,`--yes`],{stdio:`inherit`}),e.stop(`Vercel project created successfully`),!0}catch(n){return e.stop(`Failed to set up Vercel project`),console.error(t.red(`Error setting up Vercel project:`),n),!1}}async function E(){let e=s();e.start(`Pushing environment variables to Vercel...`);try{let r=m(),i=g.join(process.cwd(),r.envFile??`.env`);if(!_.default.existsSync(i))return e.stop(`No environment file found`),!0;let a=(await _.default.readFile(i,`utf-8`)).split(`
`).filter(e=>e.trim()&&!e.startsWith(`#`)).map(e=>{let[t,...n]=e.split(`=`);if(!t)return null;let r=n.join(`=`);return{key:t.trim(),value:r.trim()}}).filter(e=>e!==null);if(f.debug){e.stop(),console.log(`
Debug: Parsed environment variables:`);for(let{key:e,value:t}of a)console.log(`  ${e}=${t.substring(0,3)}...`);e.start(`Pushing environment variables to Vercel...`)}let o=0,s=a.length;for(let r=0;r<s;r++){let i=a[r];if(!i)continue;let{key:c,value:l}=i;e.message(`Pushing environment variables to Vercel... (${r+1}/${s})`);try{f.debug&&console.log(`\nDebug: Attempting to add ${c} to Vercel...`);let e=await n(`vercel`,[`env`,`add`,c,`production`],{input:l,stdio:`pipe`,reject:!1});if(f.debug&&(console.log(`Debug: Command exit code: ${e.exitCode}`),e.stdout&&console.log(`Debug: stdout:`,e.stdout),e.stderr&&console.log(`Debug: stderr:`,e.stderr)),e.exitCode!==0)throw Error(`Command failed with exit code ${e.exitCode}`)}catch(e){o++,f.debug&&(console.error(t.yellow(`\nDebug: Failed to add ${c}`)),console.error(`Debug: Full error:`,e))}}return o>0?e.stop(t.yellow(`Environment variables pushed with ${o} failures`)):e.stop(`Environment variables pushed successfully`),o<s}catch(t){return e.stop(`Failed to push environment variables`),f.debug&&(console.error(`
Debug: Top-level error in pushEnvironmentVariables:`),console.error(t)),!1}}async function D(){let e=process.version.replace(b,``).split(`.`)[0];try{let t=`.vercel/project.json`;if(!_.default.existsSync(t))return f.debug&&console.log(`Debug: No project.json found`),!1;let n=await _.default.readJSON(t);return f.debug&&console.log(`Debug: Current project config:`,n),n.settings={...n.settings,nodeVersion:`${e}.x`},await _.default.writeJSON(t,n,{spaces:2}),f.debug&&console.log(`Debug: Updated Node.js version to ${e}.x`),!0}catch(e){return f.debug&&console.error(`Debug: Failed to update Node.js version:`,e),!1}}async function O(){try{let e=await n(`vercel`,[`whoami`],{stdio:`pipe`,reject:!1});return f.debug&&console.log(`
Debug: Vercel whoami result:`,e),e.exitCode===0}catch(e){return f.debug&&console.error(`Debug: Error checking Vercel login status:`,e),!1}}async function k(){console.log(t.blue(`
You need to log in to Vercel first.`));try{return await n(`vercel`,[`login`],{stdio:`inherit`}),!0}catch(e){return console.error(t.red(`
Failed to log in to Vercel:`),e),!1}}async function A(){if(f.debug&&console.log(`Running deploy...`),!await x()&&!await S()){console.log(t.red(`
Failed to install Vercel CLI. Please install it manually using:`)),console.log(t.blue(`
  npm install -g vercel`));return}if(!await O()&&!await k()){console.log(t.red(`
Failed to log in to Vercel. Please try again.`));return}if(!await C()){if(console.log(t.blue(`
Setting up new Vercel project...`)),!await T()){console.log(t.red(`
Failed to set up Vercel project automatically.`));return}if(!await E()){console.log(t.red(`
Failed to push environment variables. Aborting deployment.`));return}}console.log(t.blue(`
Pulling latest project settings...`));try{await n(`vercel`,[`pull`,`--yes`],{stdio:`inherit`})}catch(e){console.error(t.red(`
Failed to pull project settings:`),e);return}if(await D()||console.error(t.red(`
Failed to set Node.js version. Continuing anyway...`)),f.localBuild){console.log(t.blue(`
Preparing local build for Vercel...`));try{if((await n(`vercel`,[`build`],{stdio:`inherit`,reject:!1})).exitCode===0)console.log(t.green(`
✓ Local build successful!`));else{console.error(t.red(`
✖ Local build failed`)),console.log(t.yellow(`Fix the errors above and then try again.`));return}}catch(e){console.error(t.red(`
Vercel build failed:`),e);return}console.log(t.blue(`
Deploying to Vercel...`)),(await n(`vercel`,[`deploy`,`--prebuilt`,`--yes`],{stdio:`inherit`,reject:!1})).exitCode===0&&console.log(t.green(`
✓ Deployment successful!`))}else{console.log(t.blue(`
Deploying to Vercel...`));try{if((await n(`vercel`,[`deploy`,`--yes`],{stdio:`inherit`,reject:!1})).exitCode===0)console.log(t.green(`
✓ Deployment successful!`));else{let e=h(),n=e===`npm`?`npm run`:e;console.error(t.red(`
✖ Deployment failed`)),console.log(t.yellow(`
Troubleshooting Tips:`)),console.log(t.dim(`You can check for most errors before deploying for a faster iteration cycle`)),console.log(`${t.dim(`Run`)} ${n} tsc ${t.dim(`to check for TypeScript errors (most common build errors)`)}`),console.log(`${t.dim(`Run`)} ${n} build ${t.dim(`to run the full production build locally`)}`)}}catch{return}}}const j=()=>{let e=new l(`deploy`).description(`Deploy your ProofKit application to Vercel`).addOption(u).addOption(d).addOption(new o(`--local-build`,`Build locally before deploying`)).action(A);return e.hook(`preAction`,e=>{p(e.opts()),f.baseCommand=`deploy`,c({commandName:`deploy`})}),e};export{j as makeDeployCommand,A as runDeploy};
//# sourceMappingURL=deploy-DFKoh0R9.js.map