var P=Object.defineProperty;var x=(e,n)=>P(e,"name",{value:n,configurable:!0});import{createRequire as I}from"node:module";import{I as W,T as A,e as N,E as d}from"../packem_shared/Table-3pFgIUZ2-DABgc6rj.js";import{M as B}from"../packem_shared/readFileSync-DseCu8sg-DEq4Fn3a.js";import{p as o}from"./bin.js";import{S as w,g as E,w as k,b as q,p as H,f as M,a as j,c as O,u as C,d as K,e as L,h as D}from"../packem_shared/toolchain-DoG6b_G_.js";const S=I(import.meta.url),$=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,b=x(e=>{if(typeof $<"u"&&$.versions&&$.versions.node){const[n,t]=$.versions.node.split(".").map(Number);if(n>22||n===22&&t>=3||n===20&&t>=16)return $.getBuiltinModule(e)}return S(e)},"__cjs_getBuiltinModule"),{spawnSync:F}=b("node:child_process"),{writeFileSync:U}=b("node:fs");var G=Object.defineProperty,u=x((e,n)=>G(e,"name",{value:n,configurable:!0}),"u");const y=["aube","bun","deno","go","node","npm","pnpm","python","ruby","rust","yarn"],J=u(e=>y.includes(e),"isKnownTool"),v=u(e=>e?W("✓"):A("✗"),"icon"),R=N("⚠"),z=u(e=>{if(e.installed){const n=e.version?` v${e.version}`:"",t=e.configFiles.length>0?` (${e.configFiles.join(", ")})`:"";return`${v(!0)} ${e.name}${n}${t}`}return`${R} ${e.name} — referenced by ${e.configFiles.join(", ")} but not installed`},"renderManagerLine"),Q=u(e=>{if(e.matches)return"";const{manager:n}=e;return n.name==="none"?d("→ (no manager)"):n.name==="self-activate"?"":n.installed?d(`→ ${n.name}`):d(`→ ${n.name} (missing)`)},"renderToolManager"),V=u(e=>e.matches?v(!0):e.actual?R:v(!1),"toolIcon"),X=u(e=>{if(o.info(""),o.info(d("── Toolchain ───────────────────────")),e.detected.length===0)o.info(`  ${v(!1)} No version manager detected`),o.notice(`  Install one of: ${w.join(", ")}`);else for(const n of e.detected)o.info(`  ${z(n)}`);if(o.info(""),e.tools.length===0){o.info(`  ${d("No tool pins found — add engines.node, .nvmrc, or a manager config file.")}`);return}o.info(d("── Tools ───────────────────────────"));for(const n of e.tools){const t=`${n.expected.tool} ${n.expected.version}`,a=n.actual?`actual ${n.actual}`:"not installed",i=n.matches?"":d(` [${n.expected.source}]`),s=Q(n),f=s===""?"":` ${s}`;o.info(`  ${V(n)} ${t} — ${a}${i}${f}`),n.manager.note&&o.notice(`     ${n.manager.note}`)}},"printStatus"),Y=u((e,n,t)=>{const a=E(e,n);if(t.json){process.stdout.write(`${JSON.stringify({detected:a.detected.map(s=>({binPath:s.binPath??null,configFiles:s.configFiles,installed:s.installed,name:s.name,version:s.version??null})),tools:a.tools.map(s=>({actual:s.actual??null,expected:s.expected.version,manager:s.manager.name,managerInstalled:s.manager.installed,matches:s.matches,note:s.manager.note??null,source:s.expected.source,tool:s.expected.tool}))},void 0,2)}
`);return}X(a);const i=a.tools.filter(s=>!s.matches);i.length>0&&(o.info(""),o.notice("  Run `vis toolchain install` to install pinned versions.")),t.exitCode&&i.length>0&&(process.exitCode=1)},"executeStatus"),Z=u(e=>{const n=new Map;for(const t of e){const a=n.get(t.manager.name);a?a.push(t):n.set(t.manager.name,[t])}return n},"groupByManager"),T=u((e,n,t)=>F(e,n,{cwd:t,stdio:"inherit"}).status??1,"runInvocation"),ee=u((e,n,t)=>{const a=E(e,n),i=a.tools.filter(c=>!c.matches);if(i.length===0){o.success("Everything already matches — nothing to install.");return}const s=i.some(c=>c.manager.name!=="self-activate"&&c.manager.name!=="none");if(a.detected.length===0&&s){o.error(`No version manager detected. Install one of: ${w.join(", ")}.`),process.exitCode=1;return}const f=Z(i);let l=!1,g=0;for(const[c,m]of f){if(c==="self-activate"){for(const{expected:r}of m){if(r.source==="packageManager")o.info(`${d("$")} (${r.tool} will self-activate from packageManager on next invocation)`);else if(o.info(`${d("$")} Writing packageManager=${r.tool}@${r.version}`),t.dryRun)l=!0;else try{k(e,r),l=!0}catch(h){o.error(h.message),g=1}o.notice(`  ${r.tool} ${r.version} — no install needed`)}continue}if(c==="none"){for(const{expected:r}of m)o.warn(`Cannot install ${r.tool} ${r.version} — no manager can handle it.`);g=1;continue}if(!a.detected.find(r=>r.name===c)?.installed){o.error(`${c} is referenced but not on PATH — install it first, then rerun \`vis toolchain install\`.`),g=1;continue}const _=m.map(r=>q(c,r.expected)).filter(Boolean);for(const r of _){if(!r)continue;if(r.bin==="nvm"&&r.args.length===0){o.error("nvm is a shell function — run `nvm install` in your shell, then rerun `vis toolchain install`."),r.hint&&o.notice(`  ${r.hint}`),g=1;continue}if(o.info(`${d("$")} ${r.bin} ${r.args.join(" ")}`),r.hint&&o.notice(`  ${r.hint}`),t.dryRun){l=!0;continue}const h=T(r.bin,r.args,e);if(l=!0,h!==0){g=h;break}}}if(g!==0){process.exitCode=g;return}l&&o.success("Toolchain installed.")},"executeInstall"),ne=u((e,n,t,a)=>{if(!t)throw new Error("Usage: vis toolchain use <tool>@<version> (e.g. vis toolchain use node@22.13.0)");const i=H(t);if(!i)throw new Error(`Could not parse "${t}". Expected "<tool>@<version>" where <tool> is one of ${y.join(", ")}.`);const s=M(e),f=j(i,s,n);if(f.name==="none"){o.error(`No manager can pin ${i.tool}. Install one of: ${w.join(", ")}.`),process.exitCode=1;return}if(!f.installed){o.error(`The best manager for ${i.tool} (${f.name}) is not on PATH. ${f.note??""}`),process.exitCode=1;return}const l=O(f.name,i);if(!l){o.error(`${f.name} cannot pin ${i.tool}. Use a different manager, or set \`toolchain.tools.${i.tool}\` in vis.config.ts.`),process.exitCode=1;return}if(f.name==="self-activate"){if(o.info(`${d("→")} Writing packageManager field to package.json...`),a.dryRun){o.notice(`  Would set packageManager: "${i.tool}@${i.version}"`);return}try{const c=k(e,i);if(!c){o.error(`Refusing to pin non-package-manager tool ${i.tool} via the packageManager field.`),process.exitCode=1;return}if(o.success(`Set packageManager: "${c}" — ${i.tool} will activate this version on next invocation.`),a.engines!==!1){const m=C(e,i);m&&o.success(`Updated package.json engines.${i.tool} → ${m}.`)}}catch(c){o.error(c.message),process.exitCode=1}return}if(l.args.length===0&&f.name==="nvm"&&i.tool==="node"){const c=B(e,".nvmrc");if(o.info(`${d("→")} Writing ${c}...`),a.dryRun){o.notice(`  Would write ${i.version} to .nvmrc`);return}try{U(c,`${i.version}
`)}catch(m){o.error(`Failed to write .nvmrc: ${m.message}`),process.exitCode=1;return}o.success(`Wrote ${i.version} to .nvmrc.`),o.notice("  nvm is a shell function — run `nvm use` to activate it in this shell.");return}if(l.args.length===0){o.error(`${f.name} cannot pin ${i.tool} from a subprocess. ${l.configChange?.hint??""}`),l.configChange&&o.notice(`  Edit ${l.configChange.file} by hand and rerun \`vis toolchain status\` to verify.`),process.exitCode=1;return}if(o.info(`${d("$")} ${l.bin} ${l.args.join(" ")}`),l.configChange&&o.notice(`  Will update ${l.configChange.file} — ${l.configChange.hint}`),a.dryRun)return;const g=T(l.bin,l.args,e);if(g!==0){process.exitCode=g;return}if(o.success(`Pinned ${i.tool} to ${i.version}.`),a.engines!==!1)try{const c=C(e,i);c&&o.success(`Updated package.json engines.${i.tool} → ${c}.`)}catch(c){o.warn(`Could not update engines.${i.tool}: ${c.message}`)}},"executeUse"),oe=u((e,n,t)=>{if(!t)throw new Error("Usage: vis toolchain which <tool> (e.g. vis toolchain which node)");const a=t.toLowerCase();if(!J(a))throw new Error(`Unknown tool "${t}". Known: ${y.join(", ")}.`);const i=M(e),s=j({source:"vis.config.ts",tool:a,version:"*"},i,n),f=s.installed&&s.name!=="self-activate"&&s.name!=="none"?i.find(g=>g.name===s.name):void 0,l=f?K(f,a):L(a);if(!l){o.error(`${t} not found in PATH${f?` or via ${f.name}`:""}.`),process.exitCode=1;return}process.stdout.write(`${l}
`)},"executeWhich"),te=u((e,n)=>{const t=D(e,n);process.stdout.write(`${t.name}
`)},"executeDetect"),p=u(e=>{if(!e)throw new Error("Could not determine workspace root. Run inside a monorepo.");return e},"requireRoot"),ue=u(async({options:e,visConfig:n,workspaceRoot:t})=>{Y(p(t),n?.toolchain,e)},"statusExecute"),fe=u(async({visConfig:e,workspaceRoot:n})=>{te(p(n),e?.toolchain)},"detectExecute"),de=u(async({options:e,visConfig:n,workspaceRoot:t})=>{ee(p(t),n?.toolchain,e)},"installExecute"),ge=u(async({argument:e,options:n,visConfig:t,workspaceRoot:a})=>{ne(p(a),t?.toolchain,e[0],n)},"useExecute"),me=u(async({argument:e,visConfig:n,workspaceRoot:t})=>{oe(p(t),n?.toolchain,e[0])},"whichExecute");export{fe as detectExecute,de as installExecute,ue as statusExecute,ge as useExecute,me as whichExecute};
