@bunode/cli 0.0.0-alpha.1 → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +16 -16
- package/package.json +9 -9
package/dist/cli.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{createRequire as e}from"module";import{execFile as t,execFileSync as n,spawn as r}from"child_process";import{fileURLToPath as i}from"url";import{access as a,chmod as o,copyFile as s,link as c,lstat as l,mkdir as u,readFile as d,readlink as
|
|
1
|
+
import{createRequire as e}from"module";import{execFile as t,execFileSync as n,spawn as r}from"child_process";import{fileURLToPath as i}from"url";import{access as a,chmod as o,copyFile as s,link as c,lstat as l,mkdir as u,readFile as d,readlink as ee,realpath as f,rename as p,rm as m,stat as h,unlink as te,writeFile as g}from"fs/promises";import{createHash as _,randomBytes as ne}from"crypto";import{EOL as re,homedir as v,platform as y,tmpdir as ie}from"os";import{basename as b,delimiter as ae,dirname as x,isAbsolute as oe,join as S,parse as se,resolve as C}from"path";import{constants as w,existsSync as T,readFileSync as ce}from"fs";import{promisify as le}from"util";import{arch as ue,platform as E,report as de}from"process";var fe=Object.create,pe=Object.defineProperty,me=Object.getOwnPropertyDescriptor,he=Object.getOwnPropertyNames,ge=Object.getPrototypeOf,_e=Object.prototype.hasOwnProperty,ve=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t),e=null),t.exports),ye=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=he(t),a=0,o=i.length,s;a<o;a++)s=i[a],!_e.call(e,s)&&s!==n&&pe(e,s,{get:(e=>t[e]).bind(null,s),enumerable:!(r=me(t,s))||r.enumerable});return e},be=(e,t,n)=>(n=e==null?{}:fe(ge(e)),ye(t||!e||!e.__esModule?pe(n,`default`,{value:e,enumerable:!0}):n,e)),xe=`// This script only removes its managed profile block after the owning package is uninstalled.
|
|
2
2
|
// Template literals keep the generated helper readable.
|
|
3
3
|
const crypto = require(\`node:crypto\`)
|
|
4
4
|
const fs = require(\`node:fs\`)
|
|
@@ -146,11 +146,11 @@ function cleanup() {
|
|
|
146
146
|
}
|
|
147
147
|
return true
|
|
148
148
|
}
|
|
149
|
-
`;async function xe(e,t,n){let r=v(`sha256`).update(e).digest(`hex`).slice(0,24),i=C(Se(),r),a=C(i,`cleanup-${t}-${v(`sha256`).update(n).digest(`hex`).slice(0,16)}.cjs`),o=Buffer.from(be);await u(i,{mode:448,recursive:!0});try{if((await d(a)).equals(o))return a}catch(e){if(e.code!==`ENOENT`)throw e}return await _(a,o,{mode:384}),a}function Se(){return b()===`win32`?process.env.LOCALAPPDATA??C(y(),`AppData`,`Local`):b()===`darwin`?C(y(),`Library`,`Application Support`):process.env.XDG_STATE_HOME??C(y(),`.local`,`state`)}function O(e,t,n){return Object.assign(Error(t,n),{code:e})}const k=Buffer.from([239,187,191]),A=Buffer.from([255,254]),j=Buffer.from([254,255]),Ce=Buffer.from([255,254,0,0]);function we(e){let t=`utf8`,n=e;if(e.subarray(0,4).equals(Ce))throw M();e.subarray(0,3).equals(k)?(t=`utf8-bom`,n=e.subarray(3)):e.subarray(0,2).equals(A)?(t=`utf16le`,n=e.subarray(2)):e.subarray(0,2).equals(j)&&(t=`utf16be`,n=e.subarray(2));try{return{encoding:t,text:new TextDecoder(t===`utf8-bom`?`utf8`:t===`utf16le`?`utf-16le`:t===`utf16be`?`utf-16be`:t,{fatal:!0}).decode(n)}}catch(e){throw M(e)}}function M(e){return O(`ERR_UNSUPPORTED_ENCODING`,`The shell profile uses an unsupported or invalid encoding.`,e===void 0?void 0:{cause:e})}function Te(e,t){if(t===`utf8`)return Buffer.from(e,`utf8`);if(t===`utf8-bom`)return Buffer.concat([k,Buffer.from(e,`utf8`)]);let n=Buffer.from(e,`utf16le`);if(t===`utf16le`)return Buffer.concat([A,n]);for(let e=0;e<n.length;e+=2){let t=n[e];n[e]=n[e+1],n[e+1]=t}return Buffer.concat([j,n])}async function Ee(e){let t=await ke(e);try{let[e,n]=await Promise.all([d(t),g(t)]);return{bytes:e,mode:n.mode,targetPath:t}}catch(e){if(N(e))return{bytes:Buffer.alloc(0),mode:void 0,targetPath:t};throw e}}async function De(e,t){await u(S(e.targetPath),{recursive:!0});let n=C(S(e.targetPath),`.free-shellrc-${te(8).toString(`hex`)}`);try{await _(n,t,{flag:`wx`,mode:e.mode}),e.mode!==void 0&&await o(n,e.mode),await Ae(e),await m(n,e.targetPath)}finally{await Oe(n)}}async function Oe(e){try{return await ee(e),!0}catch{return!1}}async function ke(e){try{if(!(await l(e)).isSymbolicLink())return e;try{return await p(e)}catch(t){if(!N(t))throw t;let n=await f(e);return ae(n)?n:w(S(e),n)}}catch(t){if(N(t))return e;throw t}}async function Ae(e){try{if((await d(e.targetPath)).equals(e.bytes)&&e.mode!==void 0)return}catch(t){if(N(t)&&e.mode===void 0)return;if(!N(t))throw t}throw O(`ERR_CONCURRENT_PROFILE_CHANGE`,`The shell profile changed while it was being updated: ${e.targetPath}`)}function N(e){return e.code===`ENOENT`}let P;function je(e){P=void 0;let t=Le(e),n=Re(t);if(`code`in n)return n;let r=Pe();if(!r)return{code:`UNSUPPORTED_SHELL`,message:`The current terminal is not using Bash, Zsh, Fish, Windows PowerShell, or PowerShell 7.`};let i=Ne(n.name);if(se(i))return{code:`SHELL_RESTART_REQUIRED`,message:`Restart the current shell before running this command again.`};P={entryPath:t,packageName:n.name,packagePath:n.path,restartPath:i,shell:r}}function Me(){if(P)return P;throw O(`ERR_SHELLRC_GUARD_REQUIRED`,`Call shellrcGuard(import.meta.url) at the top of the application entry before using free-shellrc.`)}function Ne(e){let t=v(`sha256`).update(e).digest(`hex`).slice(0,24);return C(re(),`.free-shellrc-${t}.restart`)}function Pe(){return(b()===`win32`?Ie():Fe())??F(process.env.SHELL)}function Fe(){try{return F(n(`ps`,[`-p`,String(process.ppid),`-o`,`comm=`],{encoding:`utf8`}).trim())}catch{return}}function Ie(){try{return F(n(`powershell.exe`,[`-NoLogo`,`-NoProfile`,`-NonInteractive`,`-Command`,`(Get-CimInstance Win32_Process -Filter 'ProcessId = ${process.ppid}').Name`],{encoding:`utf8`,windowsHide:!0}).trim())}catch{return}}function F(e){if(!e)return;let t=x(e).toLowerCase().replace(/\.exe$/,``);return t===`bash`||t===`zsh`||t===`fish`||t===`pwsh`?t:t===`powershell`?`powershell`:void 0}function Le(e){return w(e instanceof URL||e.startsWith(`file:`)?i(e):e)}function Re(e){let t=S(e),{root:n}=oe(t);for(let r=t;;r=S(r)){let t=C(r,`package.json`);if(se(t)){let n=JSON.parse(ce(t,`utf8`));return typeof n.name==`string`&&n.name.length>0?{name:n.name,path:t}:I(e)}if(r===n)return I(e)}}function I(e){return{code:`PACKAGE_NOT_FOUND`,message:`Could not find a package.json with a name for the application entry: ${e}`}}const ze=le(t);async function Be(e){if(e===`bash`)return C(L(),`.bashrc`);if(e===`zsh`)return C(R(`ZDOTDIR`,L()),`.zshrc`);if(e===`fish`)return C(R(`XDG_CONFIG_HOME`,C(L(),`.config`)),`fish`,`config.fish`);if(e===`powershell`&&b()!==`win32`)throw He(e);let t=e===`powershell`?`powershell.exe`:`pwsh`;try{let e=(await Ve(t)).trim();if(!e)throw Error(`The shell returned an empty profile path.`);return e}catch(t){throw O(`ERR_UNAVAILABLE_SHELL`,`The ${e} executable is unavailable or could not resolve its profile.`,{cause:t})}}function L(){return R(`HOME`,y())}function R(e,t){let n=process.env[e];return n?.length?n:t}function Ve(e){return ze(e,[`-NoLogo`,`-NoProfile`,`-NonInteractive`,`-Command`,`$PROFILE.CurrentUserAllHosts`],{encoding:`utf8`,windowsHide:!0}).then(e=>e.stdout)}function He(e){return O(`ERR_UNAVAILABLE_SHELL`,`The ${e} shell is unavailable on this operating system.`)}function Ue(e,t,n,r,i,a,o,s,c){let l=t.replaceAll(/\r\n|\n|\r/g,c),u=`# Please do not edit the comments \`${s.start}\`, \`${s.end}\` and the script between them, which probably makes ${s.packageName}'s feature broken.`,d=e===`fish`?Ge(l,n,r,i,a,o,s):e===`powershell`||e===`pwsh`?Ke(l,n,r,i,a,o,s):We(l,n,r,i,a,o,s);return[s.start,u,...d,s.end,``].join(c)}function We(e,t,n,r,i,a,o){return[`if [ -f ${z(t)} ] && [ -f ${z(n)} ]; then`,` command rm -f -- ${z(i)} >/dev/null 2>&1 || true`,e,`else`,` command node ${z(a)} ${z(r)} ${z(o.start)} ${z(o.end)} >/dev/null 2>&1 || true`,`fi`]}function Ge(e,t,n,r,i,a,o){return[`if test -f ${z(t)}; and test -f ${z(n)}`,` command rm -f -- ${z(i)} >/dev/null 2>&1; or true`,e,`else`,` command node ${z(a)} ${z(r)} ${z(o.start)} ${z(o.end)} >/dev/null 2>&1; or true`,`end`]}function Ke(e,t,n,r,i,a,o){return[`if ((Test-Path -LiteralPath ${B(t)} -PathType Leaf) -and (Test-Path -LiteralPath ${B(n)} -PathType Leaf)) {`,` Remove-Item -LiteralPath ${B(i)} -Force -ErrorAction SilentlyContinue`,e,`} else {`,` try {`,` & node ${B(a)} ${B(r)} ${B(o.start)} ${B(o.end)} *> $null`,` } catch {}`,`}`]}function z(e){return`'${e.replaceAll(`'`,`'"'"'`)}'`}function B(e){return`'${e.replaceAll(`'`,`''`)}'`}function qe(e){return{start:`# >>> _${e}_START >>>`,end:`# <<< _${e}_END <<<`,packageName:e}}function V(e){return/\r\n|\n|\r/.exec(e)?.[0]??ne}function Je(e,t,n){let r=H(e),i=Xe(r,t);if(i.length===0)return e.length===0?n:`${e}${V(e)}${n}`;let a=i.map((t,i)=>({end:i===0?t.end.end:Qe(t,r,e.length),start:i===0?t.start.start:Ze(t,r),value:i===0?n:``})),o=e;for(let e of a.toReversed())o=o.slice(0,e.start)+e.value+o.slice(e.end);return o}function Ye(e,t){H(e).some(e=>e.content===t.start||e.content===t.end)&&U()}function H(e){let t=[],n=/\r\n|\n|\r/g,r=0,i;for(;i=n.exec(e);)t.push({content:e.slice(r,i.index),contentEnd:i.index,end:i.index+i[0].length,start:r}),r=i.index+i[0].length;return r<e.length&&t.push({content:e.slice(r),contentEnd:e.length,end:e.length,start:r}),t}function Xe(e,t){let n=[],r;for(let i of e)i.content===t.start?(r&&U(),r=i):i.content===t.end&&(r||U(),n.push({start:r,end:i}),r=void 0);return r&&U(),n}function Ze(e,t){let n=t.find(t=>t.end===e.start.start);return n?n.content===``?n.start:n.contentEnd:e.start.start}function Qe(e,t,n){let r=e.end.end<n,i=t.find(t=>t.end===e.start.start)?.content!==``;return r&&i?e.end.contentEnd:e.end.end}function U(){throw O(`ERR_INVALID_MARKERS`,`The shell profile contains incomplete, reversed, or nested managed markers.`)}async function $e(e,t){let n=Me(),r=t??[n.shell],i=!1;for(let t of r){let r=e(t),a=await Be(t),o=await Ee(a),s=we(o.bytes),c=qe(n.packageName);Ye(r,c);let l=V(s.text),u=!s.text.split(/\r\n|\n|\r/).includes(c.start),d=await xe(n.packageName,t,a),f=Ue(t,r,n.entryPath,n.packagePath,a,n.restartPath,d,c,l),p=Te(Je(s.text,c,f),o.mode===void 0&&t===`powershell`?`utf8-bom`:s.encoding);p.equals(o.bytes)||(await De(o,p),u&&await et(n.restartPath),i=!0)}return i}async function et(e){try{await _(e,``,{flag:`wx`})}catch(e){if(e.code!==`EEXIST`)throw e}}var W=ye(_e(((e,t)=>{let n=process||{},r=n.argv||[],i=n.env||{},a=!(i.NO_COLOR||r.includes(`--no-color`))&&(!!i.FORCE_COLOR||r.includes(`--color`)||n.platform===`win32`||(n.stdout||{}).isTTY&&i.TERM!==`dumb`||!!i.CI),o=(e,t,n=e)=>r=>{let i=``+r,a=i.indexOf(t,e.length);return~a?e+s(i,t,n,a)+t:e+i+t},s=(e,t,n,r)=>{let i=``,a=0;do i+=e.substring(a,r)+n,a=r+t.length,r=e.indexOf(t,a);while(~r);return i+e.substring(a)},c=(e=a)=>{let t=e?o:()=>String;return{isColorSupported:e,reset:t(`\x1B[0m`,`\x1B[0m`),bold:t(`\x1B[1m`,`\x1B[22m`,`\x1B[22m\x1B[1m`),dim:t(`\x1B[2m`,`\x1B[22m`,`\x1B[22m\x1B[2m`),italic:t(`\x1B[3m`,`\x1B[23m`),underline:t(`\x1B[4m`,`\x1B[24m`),inverse:t(`\x1B[7m`,`\x1B[27m`),hidden:t(`\x1B[8m`,`\x1B[28m`),strikethrough:t(`\x1B[9m`,`\x1B[29m`),black:t(`\x1B[30m`,`\x1B[39m`),red:t(`\x1B[31m`,`\x1B[39m`),green:t(`\x1B[32m`,`\x1B[39m`),yellow:t(`\x1B[33m`,`\x1B[39m`),blue:t(`\x1B[34m`,`\x1B[39m`),magenta:t(`\x1B[35m`,`\x1B[39m`),cyan:t(`\x1B[36m`,`\x1B[39m`),white:t(`\x1B[37m`,`\x1B[39m`),gray:t(`\x1B[90m`,`\x1B[39m`),bgBlack:t(`\x1B[40m`,`\x1B[49m`),bgRed:t(`\x1B[41m`,`\x1B[49m`),bgGreen:t(`\x1B[42m`,`\x1B[49m`),bgYellow:t(`\x1B[43m`,`\x1B[49m`),bgBlue:t(`\x1B[44m`,`\x1B[49m`),bgMagenta:t(`\x1B[45m`,`\x1B[49m`),bgCyan:t(`\x1B[46m`,`\x1B[49m`),bgWhite:t(`\x1B[47m`,`\x1B[49m`),blackBright:t(`\x1B[90m`,`\x1B[39m`),redBright:t(`\x1B[91m`,`\x1B[39m`),greenBright:t(`\x1B[92m`,`\x1B[39m`),yellowBright:t(`\x1B[93m`,`\x1B[39m`),blueBright:t(`\x1B[94m`,`\x1B[39m`),magentaBright:t(`\x1B[95m`,`\x1B[39m`),cyanBright:t(`\x1B[96m`,`\x1B[39m`),whiteBright:t(`\x1B[97m`,`\x1B[39m`),bgBlackBright:t(`\x1B[100m`,`\x1B[49m`),bgRedBright:t(`\x1B[101m`,`\x1B[49m`),bgGreenBright:t(`\x1B[102m`,`\x1B[49m`),bgYellowBright:t(`\x1B[103m`,`\x1B[49m`),bgBlueBright:t(`\x1B[104m`,`\x1B[49m`),bgMagentaBright:t(`\x1B[105m`,`\x1B[49m`),bgCyanBright:t(`\x1B[106m`,`\x1B[49m`),bgWhiteBright:t(`\x1B[107m`,`\x1B[49m`)}};t.exports=c(),t.exports.createColors=c}))(),1);const tt=[{arch:`arm64`,name:`@bunode/cli-darwin-arm64`,os:`darwin`,target:`aarch64-apple-darwin`},{arch:`x64`,name:`@bunode/cli-darwin-x64`,os:`darwin`,target:`x86_64-apple-darwin`},{arch:`arm64`,libc:`glibc`,name:`@bunode/cli-linux-arm64-gnu`,os:`linux`,target:`aarch64-unknown-linux-gnu`},{arch:`arm64`,libc:`musl`,name:`@bunode/cli-linux-arm64-musl`,os:`linux`,target:`aarch64-unknown-linux-musl`},{arch:`x64`,libc:`glibc`,name:`@bunode/cli-linux-x64-gnu`,os:`linux`,target:`x86_64-unknown-linux-gnu`},{arch:`x64`,libc:`musl`,name:`@bunode/cli-linux-x64-musl`,os:`linux`,target:`x86_64-unknown-linux-musl`},{arch:`arm64`,name:`@bunode/cli-win32-arm64-msvc`,os:`win32`,target:`aarch64-pc-windows-msvc`},{arch:`x64`,name:`@bunode/cli-win32-x64-msvc`,os:`win32`,target:`x86_64-pc-windows-msvc`}];function nt(){let e=E===`linux`?it():void 0,t=tt.find(t=>t.os===E&&t.arch===ue&&t.libc===e);if(!t)throw Error(`Bunode does not provide a binary for ${E}-${ue}${at(e)}.`);return t}function rt(){let t=nt(),n=e(import.meta.url);try{return S(n.resolve(`${t.name}/package.json`))}catch(e){throw Error(`The optional package ${t.name} is missing. Reinstall @bunode/cli for this platform.`,{cause:e})}}function it(){let{header:e}=de.getReport();return e.glibcVersionRuntime?`glibc`:`musl`}function at(e){return e?`-${e}`:``}const G=E===`win32`;async function ot(e,t=y(),n=rt()){let r=K(t),i=G?`.exe`:``;return await u(r.binDirectory,{recursive:!0}),await q(C(n,`bunode${i}`),r.bunodeBinary),await q(C(n,`node${i}`),r.nodeBinary),G?(await J(C(r.binDirectory,`bunode.cmd`),ut(e,r.bunodeBinary)),await J(C(r.binDirectory,`bunode.ps1`),dt(e,r.bunodeBinary))):await J(C(r.binDirectory,`bunode`),lt(e,r.bunodeBinary)),r}function K(e=y()){let t=C(e,`.bunode`),n=G?`.exe`:``;return{binDirectory:C(t,`bin`),bunodeBinary:C(t,`bunode${n}`),nodeBinary:C(t,`node${n}`)}}function st(e,t){if(e===`fish`)return`fish_add_path --prepend --move ${ft(t)}`;if(e===`powershell`||e===`pwsh`){let e=G?`Path`:`PATH`;return`$env:${e} = ${Z(`${t}${ie}`)} + $env:${e}`}return`export PATH=${X(t)}:"$PATH"`}async function ct(e){try{return(await g(e)).isFile()?(await a(e,G?T.F_OK:T.X_OK),!0):!1}catch{return!1}}async function q(e,t){let n=C(S(t),`.${x(t)}.${process.pid}.${Math.random().toString(16).slice(2)}`);try{try{await c(e,n)}catch(t){if(!pt(t))throw t;await s(e,n)}G||await o(n,493),await Y(n,t)}finally{await h(n,{force:!0})}}async function J(e,t){try{if(await d(e,`utf8`)===t)return}catch(e){if(e.code!==`ENOENT`)throw e}let n=`${e}.${process.pid}.${Math.random().toString(16).slice(2)}`;try{await _(n,t,{mode:493}),await Y(n,e)}finally{await h(n,{force:!0})}}async function Y(e,t){try{await m(e,t)}catch(n){if(!G||!mt(n))throw n;await h(t,{force:!0}),await m(e,t)}}function lt(e,t){let n=C(S(t),`.wrapper-ready-`);return`#!/bin/sh
|
|
150
|
-
if [ -f ${
|
|
151
|
-
marker=${
|
|
149
|
+
`;async function Se(e,t,n){let r=_(`sha256`).update(e).digest(`hex`).slice(0,24),i=S(Ce(),r),a=S(i,`cleanup-${t}-${_(`sha256`).update(n).digest(`hex`).slice(0,16)}.cjs`),o=Buffer.from(xe);await u(i,{mode:448,recursive:!0});try{if((await d(a)).equals(o))return a}catch(e){if(e.code!==`ENOENT`)throw e}return await g(a,o,{mode:384}),a}function Ce(){return y()===`win32`?process.env.LOCALAPPDATA??S(v(),`AppData`,`Local`):y()===`darwin`?S(v(),`Library`,`Application Support`):process.env.XDG_STATE_HOME??S(v(),`.local`,`state`)}function D(e,t,n){return Object.assign(Error(t,n),{code:e})}const O=Buffer.from([239,187,191]),k=Buffer.from([255,254]),A=Buffer.from([254,255]),we=Buffer.from([255,254,0,0]);function Te(e){let t=`utf8`,n=e;if(e.subarray(0,4).equals(we))throw j();e.subarray(0,3).equals(O)?(t=`utf8-bom`,n=e.subarray(3)):e.subarray(0,2).equals(k)?(t=`utf16le`,n=e.subarray(2)):e.subarray(0,2).equals(A)&&(t=`utf16be`,n=e.subarray(2));try{return{encoding:t,text:new TextDecoder(t===`utf8-bom`?`utf8`:t===`utf16le`?`utf-16le`:t===`utf16be`?`utf-16be`:t,{fatal:!0}).decode(n)}}catch(e){throw j(e)}}function j(e){return D(`ERR_UNSUPPORTED_ENCODING`,`The shell profile uses an unsupported or invalid encoding.`,e===void 0?void 0:{cause:e})}function Ee(e,t){if(t===`utf8`)return Buffer.from(e,`utf8`);if(t===`utf8-bom`)return Buffer.concat([O,Buffer.from(e,`utf8`)]);let n=Buffer.from(e,`utf16le`);if(t===`utf16le`)return Buffer.concat([k,n]);for(let e=0;e<n.length;e+=2){let t=n[e];n[e]=n[e+1],n[e+1]=t}return Buffer.concat([A,n])}async function De(e){let t=await Ae(e);try{let[e,n]=await Promise.all([d(t),h(t)]);return{bytes:e,mode:n.mode,targetPath:t}}catch(e){if(M(e))return{bytes:Buffer.alloc(0),mode:void 0,targetPath:t};throw e}}async function Oe(e,t){await u(x(e.targetPath),{recursive:!0});let n=S(x(e.targetPath),`.free-shellrc-${ne(8).toString(`hex`)}`);try{await g(n,t,{flag:`wx`,mode:e.mode}),e.mode!==void 0&&await o(n,e.mode),await je(e),await p(n,e.targetPath)}finally{await ke(n)}}async function ke(e){try{return await te(e),!0}catch{return!1}}async function Ae(e){try{if(!(await l(e)).isSymbolicLink())return e;try{return await f(e)}catch(t){if(!M(t))throw t;let n=await ee(e);return oe(n)?n:C(x(e),n)}}catch(t){if(M(t))return e;throw t}}async function je(e){try{if((await d(e.targetPath)).equals(e.bytes)&&e.mode!==void 0)return}catch(t){if(M(t)&&e.mode===void 0)return;if(!M(t))throw t}throw D(`ERR_CONCURRENT_PROFILE_CHANGE`,`The shell profile changed while it was being updated: ${e.targetPath}`)}function M(e){return e.code===`ENOENT`}let N;function Me(e){N=void 0;let t=Re(e),n=ze(t);if(`code`in n)return n;let r=Fe();if(!r)return{code:`UNSUPPORTED_SHELL`,message:`The current terminal is not using Bash, Zsh, Fish, Windows PowerShell, or PowerShell 7.`};let i=Pe(n.name);if(T(i))return{code:`SHELL_RESTART_REQUIRED`,message:`Restart the current shell before running this command again.`};N={entryPath:t,packageName:n.name,packagePath:n.path,restartPath:i,shell:r}}function Ne(){if(N)return N;throw D(`ERR_SHELLRC_GUARD_REQUIRED`,`Call shellrcGuard(import.meta.url) at the top of the application entry before using free-shellrc.`)}function Pe(e){let t=_(`sha256`).update(e).digest(`hex`).slice(0,24);return S(ie(),`.free-shellrc-${t}.restart`)}function Fe(){return(y()===`win32`?Le():Ie())??P(process.env.SHELL)}function Ie(){try{return P(n(`ps`,[`-p`,String(process.ppid),`-o`,`comm=`],{encoding:`utf8`}).trim())}catch{return}}function Le(){try{return P(n(`powershell.exe`,[`-NoLogo`,`-NoProfile`,`-NonInteractive`,`-Command`,`(Get-CimInstance Win32_Process -Filter 'ProcessId = ${process.ppid}').Name`],{encoding:`utf8`,windowsHide:!0}).trim())}catch{return}}function P(e){if(!e)return;let t=b(e).toLowerCase().replace(/\.exe$/,``);return t===`bash`||t===`zsh`||t===`fish`||t===`pwsh`?t:t===`powershell`?`powershell`:void 0}function Re(e){return C(e instanceof URL||e.startsWith(`file:`)?i(e):e)}function ze(e){let t=x(e),{root:n}=se(t);for(let r=t;;r=x(r)){let t=S(r,`package.json`);if(T(t)){let n=JSON.parse(ce(t,`utf8`));return typeof n.name==`string`&&n.name.length>0?{name:n.name,path:t}:F(e)}if(r===n)return F(e)}}function F(e){return{code:`PACKAGE_NOT_FOUND`,message:`Could not find a package.json with a name for the application entry: ${e}`}}const Be=le(t);async function Ve(e){if(e===`bash`)return S(I(),`.bashrc`);if(e===`zsh`)return S(L(`ZDOTDIR`,I()),`.zshrc`);if(e===`fish`)return S(L(`XDG_CONFIG_HOME`,S(I(),`.config`)),`fish`,`config.fish`);if(e===`powershell`&&y()!==`win32`)throw Ue(e);let t=e===`powershell`?`powershell.exe`:`pwsh`;try{let e=(await He(t)).trim();if(!e)throw Error(`The shell returned an empty profile path.`);return e}catch(t){throw D(`ERR_UNAVAILABLE_SHELL`,`The ${e} executable is unavailable or could not resolve its profile.`,{cause:t})}}function I(){return L(`HOME`,v())}function L(e,t){let n=process.env[e];return n?.length?n:t}function He(e){return Be(e,[`-NoLogo`,`-NoProfile`,`-NonInteractive`,`-Command`,`$PROFILE.CurrentUserAllHosts`],{encoding:`utf8`,windowsHide:!0}).then(e=>e.stdout)}function Ue(e){return D(`ERR_UNAVAILABLE_SHELL`,`The ${e} shell is unavailable on this operating system.`)}function We(e,t,n,r,i,a,o,s,c){let l=t.replaceAll(/\r\n|\n|\r/g,c),u=`# Please do not edit the comments \`${s.start}\`, \`${s.end}\` and the script between them, which probably makes ${s.packageName}'s feature broken.`,d=e===`fish`?Ke(l,n,r,i,a,o,s):e===`powershell`||e===`pwsh`?qe(l,n,r,i,a,o,s):Ge(l,n,r,i,a,o,s);return[s.start,u,...d,s.end,``].join(c)}function Ge(e,t,n,r,i,a,o){return[`if [ -f ${R(t)} ] && [ -f ${R(n)} ]; then`,` command rm -f -- ${R(i)} >/dev/null 2>&1 || true`,e,`else`,` command node ${R(a)} ${R(r)} ${R(o.start)} ${R(o.end)} >/dev/null 2>&1 || true`,`fi`]}function Ke(e,t,n,r,i,a,o){return[`if test -f ${R(t)}; and test -f ${R(n)}`,` command rm -f -- ${R(i)} >/dev/null 2>&1; or true`,e,`else`,` command node ${R(a)} ${R(r)} ${R(o.start)} ${R(o.end)} >/dev/null 2>&1; or true`,`end`]}function qe(e,t,n,r,i,a,o){return[`if ((Test-Path -LiteralPath ${z(t)} -PathType Leaf) -and (Test-Path -LiteralPath ${z(n)} -PathType Leaf)) {`,` Remove-Item -LiteralPath ${z(i)} -Force -ErrorAction SilentlyContinue`,e,`} else {`,` try {`,` & node ${z(a)} ${z(r)} ${z(o.start)} ${z(o.end)} *> $null`,` } catch {}`,`}`]}function R(e){return`'${e.replaceAll(`'`,`'"'"'`)}'`}function z(e){return`'${e.replaceAll(`'`,`''`)}'`}function Je(e){return{start:`# >>> _${e}_START >>>`,end:`# <<< _${e}_END <<<`,packageName:e}}function B(e){return/\r\n|\n|\r/.exec(e)?.[0]??re}function Ye(e,t,n){let r=V(e),i=Ze(r,t);if(i.length===0)return e.length===0?n:`${e}${B(e)}${n}`;let a=i.map((t,i)=>({end:i===0?t.end.end:$e(t,r,e.length),start:i===0?t.start.start:Qe(t,r),value:i===0?n:``})),o=e;for(let e of a.toReversed())o=o.slice(0,e.start)+e.value+o.slice(e.end);return o}function Xe(e,t){V(e).some(e=>e.content===t.start||e.content===t.end)&&H()}function V(e){let t=[],n=/\r\n|\n|\r/g,r=0,i;for(;i=n.exec(e);)t.push({content:e.slice(r,i.index),contentEnd:i.index,end:i.index+i[0].length,start:r}),r=i.index+i[0].length;return r<e.length&&t.push({content:e.slice(r),contentEnd:e.length,end:e.length,start:r}),t}function Ze(e,t){let n=[],r;for(let i of e)i.content===t.start?(r&&H(),r=i):i.content===t.end&&(r||H(),n.push({start:r,end:i}),r=void 0);return r&&H(),n}function Qe(e,t){let n=t.find(t=>t.end===e.start.start);return n?n.content===``?n.start:n.contentEnd:e.start.start}function $e(e,t,n){let r=e.end.end<n,i=t.find(t=>t.end===e.start.start)?.content!==``;return r&&i?e.end.contentEnd:e.end.end}function H(){throw D(`ERR_INVALID_MARKERS`,`The shell profile contains incomplete, reversed, or nested managed markers.`)}async function et(e,t){let n=Ne(),r=t??[n.shell],i=!1;for(let t of r){let r=e(t),a=await Ve(t),o=await De(a),s=Te(o.bytes),c=Je(n.packageName);Xe(r,c);let l=B(s.text),u=!s.text.split(/\r\n|\n|\r/).includes(c.start),d=await Se(n.packageName,t,a),ee=We(t,r,n.entryPath,n.packagePath,a,n.restartPath,d,c,l),f=Ee(Ye(s.text,c,ee),o.mode===void 0&&t===`powershell`?`utf8-bom`:s.encoding);f.equals(o.bytes)||(await Oe(o,f),u&&await tt(n.restartPath),i=!0)}return i}async function tt(e){try{await g(e,``,{flag:`wx`})}catch(e){if(e.code!==`EEXIST`)throw e}}var U=be(ve(((e,t)=>{let n=process||{},r=n.argv||[],i=n.env||{},a=!(i.NO_COLOR||r.includes(`--no-color`))&&(!!i.FORCE_COLOR||r.includes(`--color`)||n.platform===`win32`||(n.stdout||{}).isTTY&&i.TERM!==`dumb`||!!i.CI),o=(e,t,n=e)=>r=>{let i=``+r,a=i.indexOf(t,e.length);return~a?e+s(i,t,n,a)+t:e+i+t},s=(e,t,n,r)=>{let i=``,a=0;do i+=e.substring(a,r)+n,a=r+t.length,r=e.indexOf(t,a);while(~r);return i+e.substring(a)},c=(e=a)=>{let t=e?o:()=>String;return{isColorSupported:e,reset:t(`\x1B[0m`,`\x1B[0m`),bold:t(`\x1B[1m`,`\x1B[22m`,`\x1B[22m\x1B[1m`),dim:t(`\x1B[2m`,`\x1B[22m`,`\x1B[22m\x1B[2m`),italic:t(`\x1B[3m`,`\x1B[23m`),underline:t(`\x1B[4m`,`\x1B[24m`),inverse:t(`\x1B[7m`,`\x1B[27m`),hidden:t(`\x1B[8m`,`\x1B[28m`),strikethrough:t(`\x1B[9m`,`\x1B[29m`),black:t(`\x1B[30m`,`\x1B[39m`),red:t(`\x1B[31m`,`\x1B[39m`),green:t(`\x1B[32m`,`\x1B[39m`),yellow:t(`\x1B[33m`,`\x1B[39m`),blue:t(`\x1B[34m`,`\x1B[39m`),magenta:t(`\x1B[35m`,`\x1B[39m`),cyan:t(`\x1B[36m`,`\x1B[39m`),white:t(`\x1B[37m`,`\x1B[39m`),gray:t(`\x1B[90m`,`\x1B[39m`),bgBlack:t(`\x1B[40m`,`\x1B[49m`),bgRed:t(`\x1B[41m`,`\x1B[49m`),bgGreen:t(`\x1B[42m`,`\x1B[49m`),bgYellow:t(`\x1B[43m`,`\x1B[49m`),bgBlue:t(`\x1B[44m`,`\x1B[49m`),bgMagenta:t(`\x1B[45m`,`\x1B[49m`),bgCyan:t(`\x1B[46m`,`\x1B[49m`),bgWhite:t(`\x1B[47m`,`\x1B[49m`),blackBright:t(`\x1B[90m`,`\x1B[39m`),redBright:t(`\x1B[91m`,`\x1B[39m`),greenBright:t(`\x1B[92m`,`\x1B[39m`),yellowBright:t(`\x1B[93m`,`\x1B[39m`),blueBright:t(`\x1B[94m`,`\x1B[39m`),magentaBright:t(`\x1B[95m`,`\x1B[39m`),cyanBright:t(`\x1B[96m`,`\x1B[39m`),whiteBright:t(`\x1B[97m`,`\x1B[39m`),bgBlackBright:t(`\x1B[100m`,`\x1B[49m`),bgRedBright:t(`\x1B[101m`,`\x1B[49m`),bgGreenBright:t(`\x1B[102m`,`\x1B[49m`),bgYellowBright:t(`\x1B[103m`,`\x1B[49m`),bgBlueBright:t(`\x1B[104m`,`\x1B[49m`),bgMagentaBright:t(`\x1B[105m`,`\x1B[49m`),bgCyanBright:t(`\x1B[106m`,`\x1B[49m`),bgWhiteBright:t(`\x1B[107m`,`\x1B[49m`)}};t.exports=c(),t.exports.createColors=c}))(),1);const nt=[{arch:`arm64`,name:`@bunode/cli-darwin-arm64`,os:`darwin`,target:`aarch64-apple-darwin`},{arch:`x64`,name:`@bunode/cli-darwin-x64`,os:`darwin`,target:`x86_64-apple-darwin`},{arch:`arm64`,libc:`glibc`,name:`@bunode/cli-linux-arm64-gnu`,os:`linux`,target:`aarch64-unknown-linux-gnu`},{arch:`arm64`,libc:`musl`,name:`@bunode/cli-linux-arm64-musl`,os:`linux`,target:`aarch64-unknown-linux-musl`},{arch:`x64`,libc:`glibc`,name:`@bunode/cli-linux-x64-gnu`,os:`linux`,target:`x86_64-unknown-linux-gnu`},{arch:`x64`,libc:`musl`,name:`@bunode/cli-linux-x64-musl`,os:`linux`,target:`x86_64-unknown-linux-musl`},{arch:`arm64`,name:`@bunode/cli-win32-arm64-msvc`,os:`win32`,target:`aarch64-pc-windows-msvc`},{arch:`x64`,name:`@bunode/cli-win32-x64-msvc`,os:`win32`,target:`x86_64-pc-windows-msvc`}];function rt(){let e=E===`linux`?at():void 0,t=nt.find(t=>t.os===E&&t.arch===ue&&t.libc===e);if(!t)throw Error(`Bunode does not provide a binary for ${E}-${ue}${ot(e)}.`);return t}function it(){let t=rt(),n=e(import.meta.url);try{return x(n.resolve(`${t.name}/package.json`))}catch(e){let n=C(import.meta.dirname,`../../..`),r=S(n,`target/debug`),i=E===`win32`?`.exe`:``;if(T(S(n,`.git`))&&T(S(r,`bunode${i}`))&&T(S(r,`node${i}`)))return r;throw Error(`The optional package ${t.name} is missing. Reinstall @bunode/cli for this platform.`,{cause:e})}}function at(){let{header:e}=de.getReport();return e.glibcVersionRuntime?`glibc`:`musl`}function ot(e){return e?`-${e}`:``}const W=E===`win32`;async function st(e,t=v(),n=it()){let r=G(t),i=W?`.exe`:``;return await u(r.binDirectory,{recursive:!0}),await ut(S(n,`bunode${i}`),r.bunodeBinary),await ut(S(n,`node${i}`),r.nodeBinary),W?(await K(S(r.binDirectory,`bunode.cmd`),ft(e,r.bunodeBinary)),await K(S(r.binDirectory,`bunode.ps1`),pt(e,r.bunodeBinary))):await K(S(r.binDirectory,`bunode`),dt(e,r.bunodeBinary)),r}function G(e=v()){let t=S(e,`.bunode`),n=W?`.exe`:``;return{binDirectory:S(t,`bin`),bunodeBinary:S(t,`bunode${n}`),nodeBinary:S(t,`node${n}`)}}function ct(e,t){if(e===`fish`)return`fish_add_path --prepend --move ${mt(t)}`;if(e===`powershell`||e===`pwsh`){let e=W?`Path`:`PATH`;return`$env:${e} = ${Y(`${t}${ae}`)} + $env:${e}`}return`export PATH=${J(t)}:"$PATH"`}async function lt(e){try{return(await h(e)).isFile()?(await a(e,W?w.F_OK:w.X_OK),!0):!1}catch{return!1}}async function ut(e,t){let n=S(x(t),`.${b(t)}.${process.pid}.${Math.random().toString(16).slice(2)}`);try{try{await c(e,n)}catch(t){if(!ht(t))throw t;await s(e,n)}W||await o(n,493),await q(n,t)}finally{await m(n,{force:!0})}}async function K(e,t){try{if(await d(e,`utf8`)===t)return}catch(e){if(e.code!==`ENOENT`)throw e}let n=`${e}.${process.pid}.${Math.random().toString(16).slice(2)}`;try{await g(n,t,{mode:493}),await q(n,e)}finally{await m(n,{force:!0})}}async function q(e,t){try{await p(e,t)}catch(n){if(!W||!gt(n))throw n;await m(t,{force:!0}),await p(e,t)}}function dt(e,t){let n=S(x(t),`.wrapper-ready-`);return`#!/bin/sh
|
|
150
|
+
if [ -f ${J(e)} ] && command -v node >/dev/null 2>&1; then
|
|
151
|
+
marker=${J(n)}$$
|
|
152
152
|
rm -f "$marker"
|
|
153
|
-
BUNODE_WRAPPER_MARKER="$marker" node ${
|
|
153
|
+
BUNODE_WRAPPER_MARKER="$marker" node ${J(e)} "$@"
|
|
154
154
|
status=$?
|
|
155
155
|
if [ "$status" -eq 0 ] || [ -f "$marker" ]; then
|
|
156
156
|
rm -f "$marker"
|
|
@@ -158,15 +158,15 @@ if [ -f ${X(e)} ] && command -v node >/dev/null 2>&1; then
|
|
|
158
158
|
fi
|
|
159
159
|
fi
|
|
160
160
|
printf '%s\n' 'bunode: JavaScript wrapper unavailable; using installed native CLI.' >&2
|
|
161
|
-
exec ${
|
|
162
|
-
`}function
|
|
161
|
+
exec ${J(t)} "$@"
|
|
162
|
+
`}function ft(e,t){return`@echo off\r
|
|
163
163
|
setlocal EnableDelayedExpansion\r
|
|
164
|
-
if exist ${
|
|
164
|
+
if exist ${X(e)} (\r
|
|
165
165
|
where node >nul 2>nul\r
|
|
166
166
|
if not errorlevel 1 (\r
|
|
167
|
-
set "BUNODE_WRAPPER_MARKER=${
|
|
167
|
+
set "BUNODE_WRAPPER_MARKER=${X(S(x(t),`.wrapper-ready-%RANDOM%-%RANDOM%`)).slice(1,-1)}"\r
|
|
168
168
|
del /q "!BUNODE_WRAPPER_MARKER!" >nul 2>nul\r
|
|
169
|
-
node ${
|
|
169
|
+
node ${X(e)} %*\r
|
|
170
170
|
set "BUNODE_WRAPPER_STATUS=!errorlevel!"\r
|
|
171
171
|
if "!BUNODE_WRAPPER_STATUS!"=="0" (\r
|
|
172
172
|
del /q "!BUNODE_WRAPPER_MARKER!" >nul 2>nul\r
|
|
@@ -179,12 +179,12 @@ if exist ${Q(e)} (\r
|
|
|
179
179
|
)\r
|
|
180
180
|
)\r
|
|
181
181
|
>&2 echo bunode: JavaScript wrapper unavailable; using installed native CLI.\r
|
|
182
|
-
${
|
|
182
|
+
${X(t)} %*\r
|
|
183
183
|
exit /b %errorlevel%\r
|
|
184
|
-
`}function
|
|
185
|
-
$marker = Join-Path ${
|
|
184
|
+
`}function pt(e,t){return`if ((Test-Path -LiteralPath ${Y(e)}) -and (Get-Command node -ErrorAction SilentlyContinue)) {
|
|
185
|
+
$marker = Join-Path ${Y(x(t))} ('.wrapper-ready-' + [guid]::NewGuid().ToString('N'))
|
|
186
186
|
$env:BUNODE_WRAPPER_MARKER = $marker
|
|
187
|
-
& node ${
|
|
187
|
+
& node ${Y(e)} @args
|
|
188
188
|
$status = $LASTEXITCODE
|
|
189
189
|
Remove-Item Env:BUNODE_WRAPPER_MARKER -ErrorAction SilentlyContinue
|
|
190
190
|
if (($status -eq 0) -or (Test-Path -LiteralPath $marker)) {
|
|
@@ -193,6 +193,6 @@ exit /b %errorlevel%\r
|
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
195
|
[Console]::Error.WriteLine('bunode: JavaScript wrapper unavailable; using installed native CLI.')
|
|
196
|
-
& ${
|
|
196
|
+
& ${Y(t)} @args
|
|
197
197
|
exit $LASTEXITCODE
|
|
198
|
-
`}function
|
|
198
|
+
`}function J(e){return`'${e.replaceAll(`'`,`'"'"'`)}'`}function mt(e){return`'${e.replaceAll(`\\`,String.raw`\\`).replaceAll(`'`,String.raw`\'`)}'`}function Y(e){return`'${e.replaceAll(`'`,`''`)}'`}function X(e){return`"${e.replaceAll(`"`,`""`)}"`}function ht(e){let{code:t}=e;return t===`EXDEV`||t===`EPERM`||t===`EACCES`||t===`ENOTSUP`}function gt(e){let{code:t}=e;return t===`EEXIST`||t===`EPERM`||t===`EACCES`}async function _t(e){let t=i(e),n=Me(e),r,a;try{let{binDirectory:e,bunodeBinary:n}=await st(t);r=e,a=n}catch(e){Q(`JavaScript wrapper failed: ${$(e)}`),await vt();return}if(n){Q(n.message),await Z(a);return}try{await et(e=>ct(e,r))&&process.stderr.write(`${U.default.green(`Bunode is ready.`)} Restart this shell to use ${r}.\n`)}catch(e){Q(`JavaScript wrapper failed: ${$(e)}`),await vt();return}await Z(a)}async function vt(){let{bunodeBinary:e}=G();if(!await lt(e)){process.stderr.write(`${U.default.red(`Bunode could not find an installed native CLI.`)}\n`),process.exitCode=1;return}Q(`Using the previously installed native Bunode CLI.`),await Z(e)}function Z(e){return new Promise((t,n)=>{let i=r(e,process.argv.slice(2),{stdio:`inherit`});i.on(`error`,n),i.on(`exit`,(e,n)=>{if(n){process.kill(process.pid,n);return}process.exitCode=e??1,t()})})}function Q(e){process.stderr.write(`${U.default.yellow(`warning:`)} ${e}\n`)}function $(e){return e instanceof Error?e.message:String(e)}export{_t as runCli};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bunode/cli",
|
|
3
|
-
"version": "0.0.0
|
|
3
|
+
"version": "0.0.0",
|
|
4
4
|
"description": "Node.js-compatible registry tooling for Bunode.",
|
|
5
5
|
"homepage": "https://github.com/liangmiQwQ/bunode#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"picocolors": "catalog:"
|
|
30
30
|
},
|
|
31
31
|
"optionalDependencies": {
|
|
32
|
-
"@bunode/cli-darwin-arm64": "0.0.0
|
|
33
|
-
"@bunode/cli-darwin-x64": "0.0.0
|
|
34
|
-
"@bunode/cli-linux-arm64-gnu": "0.0.0
|
|
35
|
-
"@bunode/cli-linux-arm64-musl": "0.0.0
|
|
36
|
-
"@bunode/cli-linux-x64-gnu": "0.0.0
|
|
37
|
-
"@bunode/cli-linux-x64-musl": "0.0.0
|
|
38
|
-
"@bunode/cli-win32-arm64-msvc": "0.0.0
|
|
39
|
-
"@bunode/cli-win32-x64-msvc": "0.0.0
|
|
32
|
+
"@bunode/cli-darwin-arm64": "0.0.0",
|
|
33
|
+
"@bunode/cli-darwin-x64": "0.0.0",
|
|
34
|
+
"@bunode/cli-linux-arm64-gnu": "0.0.0",
|
|
35
|
+
"@bunode/cli-linux-arm64-musl": "0.0.0",
|
|
36
|
+
"@bunode/cli-linux-x64-gnu": "0.0.0",
|
|
37
|
+
"@bunode/cli-linux-x64-musl": "0.0.0",
|
|
38
|
+
"@bunode/cli-win32-arm64-msvc": "0.0.0",
|
|
39
|
+
"@bunode/cli-win32-x64-msvc": "0.0.0"
|
|
40
40
|
}
|
|
41
41
|
}
|