UNPKG

clixion

Version:

A flexible framework for building CLI applications

2 lines (1 loc) 1.3 kB
import{z as l}from"./chunk-PRDJDW76.js";import{k as c}from"./chunk-XPXGDSVT.js";import{exec as f}from"node:child_process";import{spawn as g}from"cross-spawn";import x from"which";var b=n=>new Promise(t=>{f(`${n} --version`,(i,r,e)=>{t(!(i||e))})});function I(n,t,i){let r,e=new Promise(a=>{try{r=g(n,t,{cwd:i,stdio:"ignore"}),r.on("error",s=>{s&&l(String(s.message||s)),a(!1)}),r.on("close",s=>{a(s===0)})}catch{a(!1)}});return{abort:async()=>{r&&r.kill("SIGINT")},install:e}}var o=async(n,t=process.cwd(),i=process.env)=>{let r=c();if(n=Array.isArray(n)?n:[n],["exec","dlx"].includes(n[0]))switch(r){case"pnpm":case"yarn":break;case"bun":case"npm":{n=["x",...n.slice(1)];break}default:{n=["run",...n.slice(1)];break}}let e=await x(r),p=`${e} ${n.join(" ")}`;return new Promise((a,s)=>{g(e,n,{cwd:t,stdio:"inherit",env:i}).on("close",y=>{if(y!==0){s({command:p});return}a(!0)})})},M=async n=>{await o("install",n)},A=async(n,t)=>{await o(["run",...n.split(/\s+/)],t)},d=async(n,t)=>{await o(["exec",...n.split(/\s+/)],t)},m=async(n,t)=>{await o(["dlx",...n.split(/\s+/)],t)},C=async(n,t)=>{if(["pnpm","yarn"].includes(c()))try{await d(n,t)}catch{await m(n,t)}else await m(n,t)};export{I as $,o as $pm,m as $pmDlx,d as $pmExec,M as $pmInstall,A as $pmRun,C as $pmX,b as isPackageManagerInstalled};