import"./chunk-ghzc37v7.js";import{spawnSync as k}from"node:child_process";import Y from"node:fs";import y from"node:os";import W from"node:path";import V from"node:process";var S="\x1B[31m",P="\x1B[32m",b="\x1B[33m",B="\x1B[2m",M="\x1B[1m",$="\x1B[0m";function U(j){console.log(`${B}>${$} ${j}`)}function v(j){console.log(`${P}✓${$} ${j}`)}function H(j){console.log(`${b}!${$} ${j}`)}function X(j){console.error(`${S}error:${$} ${j}`)}function K(j,q,F){let A=k(j,q,{cwd:F,encoding:"utf8"});return{status:A.status??(A.error?1:0),stdout:A.stdout??"",stderr:A.stderr??(A.error?String(A.error.message):"")}}function I(j,q){let F=k("which",[j],{encoding:"utf8"});if((F.status??1)===0&&F.stdout.trim().length>0)return F.stdout.trim();let A=y.homedir(),J=[W.join(A,".local","bin",j),W.join(A,"pantry",".bin",j),W.join(A,".pantry","bin",j),W.join(A,"Code","Libraries",q,"zig-out","bin",j)];for(let N of J)if(Y.existsSync(N))return N;return null}function D(j){let F=Y.readFileSync(j,"utf8").match(/\.version\s*=\s*"([^"]+)"/);return F?F[1]:null}function T(j,q){let F=W.join(j,"package.json");if(!Y.existsSync(F))return!1;let A=Y.readFileSync(F,"utf8"),J=A.replace(/("version"\s*:\s*)"[^"]*"/,`$1"${q}"`);if(J===A)return!1;return Y.writeFileSync(F,J),!0}async function w(j){let q=j.cwd??V.cwd(),F=W.join(q,"build.zig.zon");if(!Y.existsSync(F))return X(`no build.zig.zon found in ${q}`),console.error(`  ${B}\`pantry release\` must be run from the root of a Zig package.${$}`),{exitCode:1};let A=K("git",["rev-parse","--is-inside-work-tree"],q);if(A.status!==0||A.stdout.trim()!=="true")return X("not inside a git repository"),{exitCode:1};let J=K("git",["status","--porcelain"],q);if(J.status===0&&J.stdout.trim().length>0&&!j.dryRun)return X("working tree is not clean — commit or stash your changes first."),console.error(J.stdout.trimEnd()),{exitCode:1};let N=!1;if(!j.noPush){let Q=K("git",["rev-parse","--abbrev-ref","--symbolic-full-name","@{u}"],q);if(N=Q.status===0&&Q.stdout.trim().length>0,!N)H("no upstream branch configured — will commit and tag locally (skipping push).")}let O=D(F);if(!O)return X(`could not read .version from ${F}`),{exitCode:1};let G=I("bump","zig-bump");if(!G)return X("`bump` binary not found (from zig-bump)."),console.error(`  ${B}Build it: cd ~/Code/Libraries/zig-bump && zig build, or install it onto your PATH.${$}`),{exitCode:1};if(!j.noChangelog){let Q=I("changelog","zig-changelog");if(Q)if(U("generating changelog from conventional commits..."),j.dryRun)console.log(`  ${B}[dry-run] would run: changelog -o CHANGELOG.md${$}`);else{let _=K(Q,["-o","CHANGELOG.md"],q);if(_.status===0)v("CHANGELOG.md updated");else H(`changelog generation skipped${_.stderr?`: ${_.stderr.trim()}`:""}`)}else H("`changelog` binary not found (from zig-changelog) — skipping changelog.")}if(U(`bumping version (${M}${j.type}${$}) from ${O}...`),j.dryRun){let Q=K(G,[j.type,"--dry-run","--yes"],q);if(Q.stdout.trim())console.log(Q.stdout.trimEnd());return console.log(`  ${B}[dry-run] no files changed, no commit/tag/push, no publish.${$}`),{exitCode:0}}let C=K(G,[j.type,"--no-commit","--no-tag","--no-push","--yes"],q);if(C.status!==0){if(X("version bump failed"),C.stdout.trim())console.error(C.stdout.trimEnd());if(C.stderr.trim())console.error(C.stderr.trimEnd());return{exitCode:1}}let Z=D(F);if(!Z||Z===O)return X("version did not change after bump"),{exitCode:1};v(`build.zig.zon ${O} → ${Z}`);let x=`v${Z}`;if(K("git",["rev-parse","-q","--verify",`refs/tags/${x}`],q).status===0)return X(`tag ${x} already exists — bump to a version that has no tag yet.`),K("git",["checkout","--","build.zig.zon"],q),{exitCode:1};if(T(q,Z))v(`package.json version → ${Z}`);if(K("git",["add","build.zig.zon"],q),Y.existsSync(W.join(q,"package.json")))K("git",["add","package.json"],q);if(Y.existsSync(W.join(q,"CHANGELOG.md")))K("git",["add","CHANGELOG.md"],q);let L=K("git",["commit","-m",`chore: release ${x}`],q);if(L.status!==0){if(X("git commit failed"),L.stderr.trim())console.error(L.stderr.trimEnd());return{exitCode:1}}v(`committed release ${x}`);let z=K("git",["tag","-a",x,"-m",`Release ${x}`],q);if(z.status!==0){if(X("git tag failed"),z.stderr.trim())console.error(z.stderr.trimEnd());return{exitCode:1}}if(v(`tagged ${x}`),!j.noPush&&N){U("pushing commit and tag...");let Q=K("git",["push"],q);if(Q.status!==0){if(X("git push failed"),Q.stderr.trim())console.error(Q.stderr.trimEnd());return{exitCode:1}}let _=K("git",["push","origin",x],q);if(_.status!==0){if(X(`pushing tag ${x} failed`),_.stderr.trim())console.error(_.stderr.trimEnd());return{exitCode:1}}v(`pushed commit and tag ${x}`)}else H("skipped push (commit and tag are local only).");if(!j.noPublish)R(q);return console.log(`
${P}${M}✓ released ${x}${$}`),{exitCode:0}}function R(j){let q=I("pantry","pantry");if(!q){H("`pantry` binary not found — skipping publish (the pushed tag's CI will publish).");return}if(K(q,["publish","--help"],j).status!==0){H("`pantry publish` is not available in the installed pantry yet — skipping."),console.log(`  ${B}The pushed tag will trigger CI to publish to the registry.${$}`);return}U("publishing to pantry registry...");let A=K(q,["publish"],j);if(A.stdout.trim())console.log(A.stdout.trimEnd());if(A.status===0)v("published to pantry registry");else if(H("publish step failed — the pushed tag's CI can still publish."),A.stderr.trim())console.log(`  ${B}${A.stderr.trim()}${$}`)}export{w as runRelease};
