-
Notifications
You must be signed in to change notification settings - Fork 772
Expand file tree
/
Copy pathcli-acp.js
More file actions
executable file
·105 lines (89 loc) · 2.6 KB
/
Copy pathcli-acp.js
File metadata and controls
executable file
·105 lines (89 loc) · 2.6 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
#!/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}`]
if (platform === 'win32' && arch === 'arm64') {
candidates.push('@shareai-lab/kode-bin-win32-x64')
}
for (const pkgName of candidates) {
try {
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 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 || ''
const args = ['--acp', ...process.argv.slice(2)]
// Native binary (npm optionalDependencies, no GitHub postinstall).
const nativeBin = tryResolveNativeBinaryFromOptionalDeps()
if (nativeBin) {
run(nativeBin, args)
}
// Node.js runtime fallback.
const distEntry = path.join(packageRoot, 'dist', 'index.js')
if (fs.existsSync(distEntry)) {
run(process.execPath, [distEntry, ...args])
}
process.stderr.write(
[
'❌ kode-acp is not runnable on this system.',
'',
'Tried:',
'- Native binary (optionalDependencies)',
'- Node.js runtime fallback',
'',
'Fix:',
'- Reinstall with optionalDependencies enabled (avoid --no-optional/--omit=optional)',
'- Or install a platform binary package: @shareai-lab/kode-bin-<platform>-<arch>',
'- Or run from source: bun run apps/cli/src/dispatch.ts --acp',
'',
version ? `Package version: ${version}` : '',
]
.filter(Boolean)
.join('\n'),
)
process.exit(1)
}
if (require.main === module) {
main()
}
module.exports = { main }