-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·130 lines (111 loc) · 3.41 KB
/
Copy pathcli.js
File metadata and controls
executable file
·130 lines (111 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
const fs = require('node:fs')
const path = require('node:path')
const { spawnSync } = require('node:child_process')
function tryResolveNativeBinaryFromOptionalDeps() {
const platform = process.platform
const arch = process.arch
const candidates = [`@shareai-lab/kode-bin-${platform}-${arch}`]
// Windows ARM64 can usually run x64 binaries via emulation, so offer a fallback.
if (platform === 'win32' && arch === 'arm64') {
candidates.push('@shareai-lab/kode-bin-win32-x64')
}
for (const pkgName of candidates) {
try {
// eslint-disable-next-line import/no-dynamic-require
const mod = require(pkgName)
const binPath = mod?.kodePath
if (typeof binPath === 'string' && fs.existsSync(binPath)) {
return binPath
}
} catch {}
}
return null
}
function findPackageRoot(startDir) {
let dir = startDir
for (let i = 0; i < 25; i++) {
if (fs.existsSync(path.join(dir, 'package.json'))) return dir
const parent = path.dirname(dir)
if (parent === dir) break
dir = parent
}
return startDir
}
function readPackageJson(packageRoot) {
try {
const p = path.join(packageRoot, 'package.json')
return JSON.parse(fs.readFileSync(p, 'utf8'))
} catch {
return null
}
}
function hasFlag(flag) {
return process.argv.includes(flag)
}
function printHelpLite() {
process.stdout.write(
`Usage: kode [options] [command] [prompt]\n\n` +
`Common options:\n` +
` -h, --help Show full help\n` +
` -v, --version Show version\n` +
` -p, --print Print response and exit (non-interactive)\n` +
` -c, --cwd <cwd> Set working directory\n`,
)
}
function run(cmd, args) {
const result = spawnSync(cmd, args, {
stdio: 'inherit',
env: { ...process.env, KODE_PACKAGED: process.env.KODE_PACKAGED || '1' },
})
if (result.error) {
throw result.error
}
process.exit(typeof result.status === 'number' ? result.status : 1)
}
function main() {
const packageRoot = findPackageRoot(__dirname)
const pkg = readPackageJson(packageRoot)
const version = pkg?.version || ''
if (hasFlag('--help-lite')) {
printHelpLite()
process.exit(0)
}
if (hasFlag('--version') || hasFlag('-v')) {
process.stdout.write(`${version}\n`)
process.exit(0)
}
// Native binary (npm optionalDependencies, no GitHub postinstall).
const nativeBin = tryResolveNativeBinaryFromOptionalDeps()
if (nativeBin) {
run(nativeBin, process.argv.slice(2))
}
// Node.js runtime fallback.
const distEntry = path.join(packageRoot, 'dist', 'index.js')
if (fs.existsSync(distEntry)) {
run(process.execPath, [distEntry, ...process.argv.slice(2)])
}
// Final fallback: explain what to do
process.stderr.write(
[
'❌ Kode is not runnable on this system.',
'',
'Tried:',
'- Native binary (optionalDependencies)',
'- Node.js runtime (dist/index.js)',
'',
'Fix:',
'- Reinstall with optionalDependencies enabled (avoid --no-optional/--omit=optional)',
'- Or install a platform binary package: @shareai-lab/kode-bin-<platform>-<arch>',
'- Or reinstall and ensure dist/ is present (npm install -g @shareai-lab/kode)',
'- Or run from source: bun run dev',
'',
version ? `Package version: ${version}` : '',
].join('\n'),
)
process.exit(1)
}
if (require.main === module) {
main()
}
module.exports = { main }