forked from claude-code-best/claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-wrapper.cjs
More file actions
103 lines (96 loc) · 2.99 KB
/
Copy pathcli-wrapper.cjs
File metadata and controls
103 lines (96 loc) · 2.99 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
#!/usr/bin/env node
// Fallback launcher for the claude wrapper package.
//
// Normally the postinstall script copies the native binary over bin/claude.exe,
// so this file is never invoked. It exists for environments where postinstall
// doesn't run (--ignore-scripts).
const { spawnSync } = require('child_process')
const { arch, constants } = require('os')
const path = require('path')
const PACKAGE_PREFIX = '@go-hare/claude-code'
const BINARY_NAME = 'claude'
const WRAPPER_NAME = require('./package.json').name
const PLATFORMS = {
'darwin-arm64': { pkg: PACKAGE_PREFIX + '-darwin-arm64', bin: BINARY_NAME },
'darwin-x64': { pkg: PACKAGE_PREFIX + '-darwin-x64', bin: BINARY_NAME },
'linux-x64': { pkg: PACKAGE_PREFIX + '-linux-x64', bin: BINARY_NAME },
'linux-arm64': { pkg: PACKAGE_PREFIX + '-linux-arm64', bin: BINARY_NAME },
'linux-x64-musl': {
pkg: PACKAGE_PREFIX + '-linux-x64-musl',
bin: BINARY_NAME,
},
'linux-arm64-musl': {
pkg: PACKAGE_PREFIX + '-linux-arm64-musl',
bin: BINARY_NAME,
},
'win32-x64': {
pkg: PACKAGE_PREFIX + '-win32-x64',
bin: BINARY_NAME + '.exe',
},
'win32-arm64': {
pkg: PACKAGE_PREFIX + '-win32-arm64',
bin: BINARY_NAME + '.exe',
},
}
function detectMusl() {
if (process.platform !== 'linux') return false
const report =
typeof process.report?.getReport === 'function'
? process.report.getReport()
: null
return report != null && report.header?.glibcVersionRuntime === undefined
}
function getPlatformKey() {
const platform = process.platform
let cpu = arch()
if (platform === 'linux')
return 'linux-' + cpu + (detectMusl() ? '-musl' : '')
if (platform === 'darwin' && cpu === 'x64') {
const r = spawnSync('sysctl', ['-n', 'sysctl.proc_translated'], {
encoding: 'utf8',
})
if (r.stdout?.trim() === '1') cpu = 'arm64'
}
return platform + '-' + cpu
}
function getBinaryPath() {
const platformKey = getPlatformKey()
const info = PLATFORMS[platformKey]
if (!info) {
console.error(
`[${WRAPPER_NAME}] Unsupported platform: ${process.platform} ${arch()}`,
)
process.exit(1)
}
try {
const pkgDir = path.dirname(require.resolve(info.pkg + '/package.json'))
return path.join(pkgDir, info.bin)
} catch {
console.error(
`[${WRAPPER_NAME}] Could not find native binary package "${info.pkg}".`,
)
console.error(' Try reinstalling with: npm install')
process.exit(1)
}
}
function main() {
const binaryPath = getBinaryPath()
const result = spawnSync(binaryPath, process.argv.slice(2), {
stdio: 'inherit',
env: { ...process.env, CLAUDE_CODE_INSTALLED_VIA_NPM_WRAPPER: '1' },
})
if (result.error) {
console.error(
`[${WRAPPER_NAME}] Failed to execute native binary at ` + binaryPath,
)
console.error(' ' + result.error.message)
process.exit(1)
}
if (result.signal) {
const signum = constants.signals[result.signal] ?? 0
process.exit(128 + signum)
} else {
process.exit(result.status ?? 1)
}
}
main()