@bunode/cli 0.0.0-alpha.0 → 0.0.0-alpha.1

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/bin/bunode.mjs ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ const wrapperMarker = process.env.BUNODE_WRAPPER_MARKER
3
+
4
+ try {
5
+ const { runCli } = await import('../dist/cli.mjs')
6
+
7
+ if (wrapperMarker) {
8
+ delete process.env.BUNODE_WRAPPER_MARKER
9
+ try {
10
+ const { writeFile } = await import('node:fs/promises')
11
+ await writeFile(wrapperMarker, '')
12
+ } catch {
13
+ // The launcher treats a missing marker as a wrapper startup failure.
14
+ }
15
+ }
16
+
17
+ await runCli(import.meta.url)
18
+ } catch (error) {
19
+ process.stderr.write(`Bunode failed: ${getErrorMessage(error)}\n`)
20
+ process.exitCode = 1
21
+ }
22
+
23
+ function getErrorMessage(error) {
24
+ return error instanceof Error ? error.message : String(error)
25
+ }
package/dist/cli.mjs CHANGED
@@ -1,3 +1,198 @@
1
- #!/usr/bin/env node
2
- process.stdout.write(`helloworld
3
- `);export{};
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 f,realpath as p,rename as m,rm as h,stat as g,unlink as ee,writeFile as _}from"fs/promises";import{createHash as v,randomBytes as te}from"crypto";import{EOL as ne,homedir as y,platform as b,tmpdir as re}from"os";import{basename as x,delimiter as ie,dirname as S,isAbsolute as ae,join as C,parse as oe,resolve as w}from"path";import{constants as T,existsSync as se,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,D=Object.defineProperty,pe=Object.getOwnPropertyDescriptor,me=Object.getOwnPropertyNames,he=Object.getPrototypeOf,ge=Object.prototype.hasOwnProperty,_e=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t),e=null),t.exports),ve=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=me(t),a=0,o=i.length,s;a<o;a++)s=i[a],!ge.call(e,s)&&s!==n&&D(e,s,{get:(e=>t[e]).bind(null,s),enumerable:!(r=pe(t,s))||r.enumerable});return e},ye=(e,t,n)=>(n=e==null?{}:fe(he(e)),ve(t||!e||!e.__esModule?D(n,`default`,{value:e,enumerable:!0}):n,e)),be=`// This script only removes its managed profile block after the owning package is uninstalled.
2
+ // Template literals keep the generated helper readable.
3
+ const crypto = require(\`node:crypto\`)
4
+ const fs = require(\`node:fs\`)
5
+ const path = require(\`node:path\`)
6
+
7
+ const [profile, startMarker, endMarker] = process.argv.slice(2)
8
+
9
+ if (cleanup()) {
10
+ try {
11
+ fs.unlinkSync(__filename)
12
+ fs.rmdirSync(path.dirname(__filename))
13
+ } catch {
14
+ /* Self-removal failure must not affect profile cleanup. */
15
+ }
16
+ }
17
+
18
+ function cleanup() {
19
+ /* 1. Read and decode the profile without losing its original encoding. */
20
+ const link = fs.lstatSync(profile).isSymbolicLink()
21
+ const target = link ? fs.realpathSync(profile) : profile
22
+ const original = fs.readFileSync(target)
23
+ const stat = fs.statSync(target)
24
+
25
+ let encoding = \`utf8\`
26
+ let offset = 0
27
+ if (original.subarray(0, 3).equals(Buffer.from([239, 187, 191]))) {
28
+ encoding = \`utf8-bom\`
29
+ offset = 3
30
+ } else if (original.subarray(0, 2).equals(Buffer.from([255, 254]))) {
31
+ encoding = \`utf16le\`
32
+ offset = 2
33
+ } else if (original.subarray(0, 2).equals(Buffer.from([254, 255]))) {
34
+ encoding = \`utf16be\`
35
+ offset = 2
36
+ }
37
+
38
+ const decoderEncoding =
39
+ encoding === \`utf8-bom\`
40
+ ? \`utf8\`
41
+ : encoding === \`utf16le\`
42
+ ? \`utf-16le\`
43
+ : encoding === \`utf16be\`
44
+ ? \`utf-16be\`
45
+ : encoding
46
+ const decoder = new TextDecoder(decoderEncoding, { fatal: true })
47
+ const text = decoder.decode(original.subarray(offset))
48
+ const lines = []
49
+ const lineEnding = /\\r\\n|\\n|\\r/g
50
+ let lineStart = 0
51
+ let match
52
+
53
+ /* 2. Locate only complete, exact managed blocks. */
54
+ while ((match = lineEnding.exec(text))) {
55
+ lines.push({
56
+ content: text.slice(lineStart, match.index),
57
+ contentEnd: match.index,
58
+ end: match.index + match[0].length,
59
+ start: lineStart
60
+ })
61
+ lineStart = match.index + match[0].length
62
+ }
63
+ if (lineStart < text.length) {
64
+ lines.push({
65
+ content: text.slice(lineStart),
66
+ contentEnd: text.length,
67
+ end: text.length,
68
+ start: lineStart
69
+ })
70
+ }
71
+
72
+ const blocks = []
73
+ let opening
74
+ for (const line of lines) {
75
+ if (line.content === startMarker) {
76
+ if (opening) {
77
+ return false
78
+ }
79
+ opening = line
80
+ } else if (line.content === endMarker) {
81
+ if (!opening) {
82
+ return false
83
+ }
84
+ blocks.push({ start: opening, end: line })
85
+ opening = undefined
86
+ }
87
+ }
88
+ if (opening || blocks.length === 0) {
89
+ return false
90
+ }
91
+
92
+ /* 3. Remove managed blocks while preserving every other character. */
93
+ let updated = text
94
+ for (const block of blocks.toReversed()) {
95
+ const previous = lines.find(line => line.end === block.start.start)
96
+ const from = previous
97
+ ? previous.content.length === 0
98
+ ? previous.start
99
+ : previous.contentEnd
100
+ : block.start.start
101
+ const to =
102
+ block.end.end < text.length && previous && previous.content.length > 0
103
+ ? block.end.contentEnd
104
+ : block.end.end
105
+ updated = updated.slice(0, from) + updated.slice(to)
106
+ }
107
+
108
+ /* 4. Restore the original byte encoding and byte-order mark. */
109
+ let content = Buffer.from(updated, encoding.startsWith(\`utf16\`) ? \`utf16le\` : \`utf8\`)
110
+ if (encoding === \`utf16be\`) {
111
+ for (let index = 0; index < content.length; index += 2) {
112
+ const first = content[index]
113
+ content[index] = content[index + 1]
114
+ content[index + 1] = first
115
+ }
116
+ }
117
+ if (encoding === \`utf8-bom\`) {
118
+ content = Buffer.concat([Buffer.from([239, 187, 191]), content])
119
+ } else if (encoding === \`utf16le\`) {
120
+ content = Buffer.concat([Buffer.from([255, 254]), content])
121
+ } else if (encoding === \`utf16be\`) {
122
+ content = Buffer.concat([Buffer.from([254, 255]), content])
123
+ }
124
+
125
+ /* 5. Replace the resolved target only if it has not changed. */
126
+ if (!fs.readFileSync(target).equals(original)) {
127
+ return false
128
+ }
129
+ const temporary = path.join(
130
+ path.dirname(target),
131
+ \`.free-shellrc-\${crypto.randomBytes(8).toString(\`hex\`)}\`
132
+ )
133
+ try {
134
+ fs.writeFileSync(temporary, content, { flag: \`wx\`, mode: stat.mode })
135
+ fs.chmodSync(temporary, stat.mode)
136
+ if (!fs.readFileSync(target).equals(original)) {
137
+ throw new Error(\`Concurrent profile change\`)
138
+ }
139
+ fs.renameSync(temporary, target)
140
+ } finally {
141
+ try {
142
+ fs.unlinkSync(temporary)
143
+ } catch {
144
+ /* Cleanup failure must not hide the original result. */
145
+ }
146
+ }
147
+ return true
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 ${X(e)} ] && command -v node >/dev/null 2>&1; then
151
+ marker=${X(n)}$$
152
+ rm -f "$marker"
153
+ BUNODE_WRAPPER_MARKER="$marker" node ${X(e)} "$@"
154
+ status=$?
155
+ if [ "$status" -eq 0 ] || [ -f "$marker" ]; then
156
+ rm -f "$marker"
157
+ exit "$status"
158
+ fi
159
+ fi
160
+ printf '%s\n' 'bunode: JavaScript wrapper unavailable; using installed native CLI.' >&2
161
+ exec ${X(t)} "$@"
162
+ `}function ut(e,t){return`@echo off\r
163
+ setlocal EnableDelayedExpansion\r
164
+ if exist ${Q(e)} (\r
165
+ where node >nul 2>nul\r
166
+ if not errorlevel 1 (\r
167
+ set "BUNODE_WRAPPER_MARKER=${Q(C(S(t),`.wrapper-ready-%RANDOM%-%RANDOM%`)).slice(1,-1)}"\r
168
+ del /q "!BUNODE_WRAPPER_MARKER!" >nul 2>nul\r
169
+ node ${Q(e)} %*\r
170
+ set "BUNODE_WRAPPER_STATUS=!errorlevel!"\r
171
+ if "!BUNODE_WRAPPER_STATUS!"=="0" (\r
172
+ del /q "!BUNODE_WRAPPER_MARKER!" >nul 2>nul\r
173
+ exit /b 0\r
174
+ )\r
175
+ if exist "!BUNODE_WRAPPER_MARKER!" (\r
176
+ del /q "!BUNODE_WRAPPER_MARKER!" >nul 2>nul\r
177
+ exit /b !BUNODE_WRAPPER_STATUS!\r
178
+ )\r
179
+ )\r
180
+ )\r
181
+ >&2 echo bunode: JavaScript wrapper unavailable; using installed native CLI.\r
182
+ ${Q(t)} %*\r
183
+ exit /b %errorlevel%\r
184
+ `}function dt(e,t){return`if ((Test-Path -LiteralPath ${Z(e)}) -and (Get-Command node -ErrorAction SilentlyContinue)) {
185
+ $marker = Join-Path ${Z(S(t))} ('.wrapper-ready-' + [guid]::NewGuid().ToString('N'))
186
+ $env:BUNODE_WRAPPER_MARKER = $marker
187
+ & node ${Z(e)} @args
188
+ $status = $LASTEXITCODE
189
+ Remove-Item Env:BUNODE_WRAPPER_MARKER -ErrorAction SilentlyContinue
190
+ if (($status -eq 0) -or (Test-Path -LiteralPath $marker)) {
191
+ Remove-Item -LiteralPath $marker -Force -ErrorAction SilentlyContinue
192
+ exit $status
193
+ }
194
+ }
195
+ [Console]::Error.WriteLine('bunode: JavaScript wrapper unavailable; using installed native CLI.')
196
+ & ${Z(t)} @args
197
+ exit $LASTEXITCODE
198
+ `}function X(e){return`'${e.replaceAll(`'`,`'"'"'`)}'`}function ft(e){return`'${e.replaceAll(`\\`,String.raw`\\`).replaceAll(`'`,String.raw`\'`)}'`}function Z(e){return`'${e.replaceAll(`'`,`''`)}'`}function Q(e){return`"${e.replaceAll(`"`,`""`)}"`}function pt(e){let{code:t}=e;return t===`EXDEV`||t===`EPERM`||t===`EACCES`||t===`ENOTSUP`}function mt(e){let{code:t}=e;return t===`EEXIST`||t===`EPERM`||t===`EACCES`}async function ht(e){let t=i(e),n=je(e);if(n){$(n.message),await gt();return}let r;try{let{binDirectory:e,bunodeBinary:n}=await ot(t);await $e(t=>st(t,e))&&process.stderr.write(`${W.default.green(`Bunode is ready.`)} Restart this shell to use ${e}.\n`),r=n}catch(e){$(`JavaScript wrapper failed: ${vt(e)}`),await gt();return}await _t(r)}async function gt(){let{bunodeBinary:e}=K();if(!await ct(e)){process.stderr.write(`${W.default.red(`Bunode could not find an installed native CLI.`)}\n`),process.exitCode=1;return}$(`Using the previously installed native Bunode CLI.`),await _t(e)}function _t(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 $(e){process.stderr.write(`${W.default.yellow(`warning:`)} ${e}\n`)}function vt(e){return e instanceof Error?e.message:String(e)}export{ht as runCli};
package/package.json CHANGED
@@ -1,18 +1,41 @@
1
1
  {
2
2
  "name": "@bunode/cli",
3
- "version": "0.0.0-alpha.0",
3
+ "version": "0.0.0-alpha.1",
4
4
  "description": "Node.js-compatible registry tooling for Bunode.",
5
+ "homepage": "https://github.com/liangmiQwQ/bunode#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/liangmiQwQ/bunode/issues"
8
+ },
9
+ "license": "MIT",
10
+ "author": "Liang Mi <hi@liangmi.dev>",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/liangmiQwQ/bunode.git"
14
+ },
5
15
  "bin": {
6
- "bunode-registry": "./dist/cli.mjs"
16
+ "bunode": "bin/bunode.mjs"
7
17
  },
8
18
  "files": [
9
- "dist"
19
+ "dist",
20
+ "bin"
10
21
  ],
11
22
  "type": "module",
12
- "scripts": {
13
- "build": "vp pack",
14
- "check": "vp check",
15
- "format": "vp format",
16
- "lint": "vp lint"
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "catalog:",
28
+ "free-shellrc": "catalog:",
29
+ "picocolors": "catalog:"
30
+ },
31
+ "optionalDependencies": {
32
+ "@bunode/cli-darwin-arm64": "0.0.0-alpha.1",
33
+ "@bunode/cli-darwin-x64": "0.0.0-alpha.1",
34
+ "@bunode/cli-linux-arm64-gnu": "0.0.0-alpha.1",
35
+ "@bunode/cli-linux-arm64-musl": "0.0.0-alpha.1",
36
+ "@bunode/cli-linux-x64-gnu": "0.0.0-alpha.1",
37
+ "@bunode/cli-linux-x64-musl": "0.0.0-alpha.1",
38
+ "@bunode/cli-win32-arm64-msvc": "0.0.0-alpha.1",
39
+ "@bunode/cli-win32-x64-msvc": "0.0.0-alpha.1"
17
40
  }
18
- }
41
+ }