@shareai-lab/kode 1.0.92 → 1.0.94

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.
Files changed (2) hide show
  1. package/cli.js +58 -69
  2. package/package.json +1 -1
package/cli.js CHANGED
@@ -3,16 +3,10 @@
3
3
  const { spawn } = require('child_process');
4
4
  const path = require('path');
5
5
 
6
- // Prefer Bun for dev; otherwise use Node + tsx (ESM --import)
6
+ // Prefer Bun if available for speed; otherwise use Node + tsx
7
7
  const args = process.argv.slice(2);
8
8
  const cliPath = path.join(__dirname, 'src', 'entrypoints', 'cli.tsx');
9
9
 
10
- // Resolve tsx from this package directory (avoid CWD-based resolution on Windows/global installs)
11
- let tsxImportPath = null;
12
- let tsxCliPath = null;
13
- try { tsxImportPath = require.resolve('tsx', { paths: [__dirname] }); } catch {}
14
- try { tsxCliPath = require.resolve('tsx/cli', { paths: [__dirname] }); } catch {}
15
-
16
10
  // Enforce minimum Node version based on package.json engines.node
17
11
  try {
18
12
  const req = "${enginesNode}"
@@ -28,73 +22,68 @@ try {
28
22
  }
29
23
  } catch {}
30
24
 
31
- // Try Bun first
32
- try {
33
- const { execSync } = require('child_process');
34
- execSync('bun --version', { stdio: 'ignore' });
35
- const child = spawn('bun', ['run', cliPath, ...args], {
25
+ // Find packaged Bun binary or fall back to system Bun
26
+ function hasBun() {
27
+ try {
28
+ require('child_process').execSync('bun --version', { stdio: 'ignore' })
29
+ return true
30
+ } catch { return false }
31
+ }
32
+
33
+ function runWithBun() {
34
+ const bunCmd = process.platform === 'win32' ? 'bun.cmd' : 'bun'
35
+ const child = spawn(bunCmd, ['run', cliPath, ...args], {
36
36
  stdio: 'inherit',
37
37
  env: { ...process.env, YOGA_WASM_PATH: path.join(__dirname, 'yoga.wasm') },
38
- });
39
- child.on('exit', (code) => process.exit(code || 0));
40
- child.on('error', () => runWithNode());
41
- } catch { runWithNode(); }
38
+ })
39
+ child.on('error', () => runWithTsxCLI())
40
+ child.on('exit', code => {
41
+ if (code && code !== 0) return runWithTsxCLI()
42
+ process.exit(code || 0)
43
+ })
44
+ }
42
45
 
43
- function runWithNode() {
46
+ function runWithTsxImport() {
47
+ let importArg = 'tsx'
48
+ try {
49
+ if (tsxImportPath) {
50
+ const { pathToFileURL } = require('node:url')
51
+ importArg = pathToFileURL(tsxImportPath).href
52
+ }
53
+ } catch {}
44
54
  const baseArgs = ['--no-warnings=ExperimentalWarning', '--enable-source-maps']
55
+ const child = spawn(process.execPath, [...baseArgs, '--import', importArg, cliPath, ...args], {
56
+ stdio: 'inherit',
57
+ env: { ...process.env, YOGA_WASM_PATH: path.join(__dirname, 'yoga.wasm') },
58
+ })
59
+ child.on('error', () => runWithTsxCLI())
60
+ child.on('exit', code => {
61
+ if (code && code !== 0) return runWithTsxCLI()
62
+ process.exit(code || 0)
63
+ })
64
+ }
45
65
 
46
- const tryWithImport = () => {
47
- let importArg = 'tsx'
48
- try {
49
- if (tsxImportPath) {
50
- const { pathToFileURL } = require('node:url')
51
- importArg = pathToFileURL(tsxImportPath).href
52
- }
53
- } catch {}
54
- const child = spawn(process.execPath, [...baseArgs, '--import', importArg, cliPath, ...args], {
55
- stdio: 'inherit',
56
- env: { ...process.env, YOGA_WASM_PATH: path.join(__dirname, 'yoga.wasm') },
57
- })
58
- child.on('error', () => failWithTsxHint())
59
- child.on('exit', code => {
60
- if (code && code !== 0) return failWithTsxHint()
61
- process.exit(code || 0)
62
- })
63
- }
64
-
65
- const tryWithTsxBinary = () => {
66
- if (!tsxCliPath) return failWithTsxHint()
67
- const child = spawn(process.execPath, [tsxCliPath, cliPath, ...args], {
68
- stdio: 'inherit',
69
- env: { ...process.env, YOGA_WASM_PATH: path.join(__dirname, 'yoga.wasm') },
70
- })
71
- child.on('error', () => failWithTsxHint())
72
- child.on('exit', code => {
73
- if (code && code !== 0) return failWithTsxHint()
74
- process.exit(code || 0)
75
- })
76
- }
66
+ function runWithTsxCLI() {
67
+ if (!tsxCliPath) return fail()
68
+ const child = spawn(process.execPath, [tsxCliPath, cliPath, ...args], {
69
+ stdio: 'inherit',
70
+ env: { ...process.env, YOGA_WASM_PATH: path.join(__dirname, 'yoga.wasm') },
71
+ })
72
+ child.on('error', () => fail())
73
+ child.on('exit', code => process.exit(code || 0))
74
+ }
77
75
 
78
- function failWithTsxHint() {
79
- console.error('')
80
- console.error('Failed to launch with Node + tsx.')
81
- console.error('Try reinstalling: npm i -g @shareai-lab/kode (ensures bundled tsx).')
82
- console.error('Or install Bun and run: bun run kode')
83
- process.exit(1)
84
- }
76
+ function fail() {
77
+ console.error('')
78
+ console.error('Failed to start Kode.')
79
+ console.error('Try installing Bun: npm i -g bun, or ensure tsx is available.')
80
+ process.exit(1)
81
+ }
85
82
 
86
- // Windows prefers tsx CLI due to top-level await in CJS transforms
87
- if (process.platform === 'win32') {
88
- tryWithTsxBinary()
89
- } else {
90
- // Non-Windows: try import-based first, then fallback
91
- let attempted = false
92
- const child = spawn(process.execPath, [...baseArgs, '--eval', ''], { stdio: 'ignore' })
93
- child.on('error', () => {})
94
- child.on('exit', () => {
95
- if (attempted) return
96
- attempted = true
97
- tryWithImport()
98
- })
99
- }
83
+ if (process.platform === 'win32') {
84
+ // On Windows, tsx CLI is the most reliable
85
+ runWithTsxCLI()
86
+ } else {
87
+ if (hasBun()) runWithBun()
88
+ else runWithTsxImport()
100
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shareai-lab/kode",
3
- "version": "1.0.92",
3
+ "version": "1.0.94",
4
4
  "bin": {
5
5
  "kode": "cli.js",
6
6
  "kwa": "cli.js",