forked from claude-code-best/claude-code
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.cjs
More file actions
250 lines (231 loc) · 7.28 KB
/
Copy pathinstall.cjs
File metadata and controls
250 lines (231 loc) · 7.28 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env node
// Postinstall for the claude wrapper package.
//
// In development (monorepo with .git), delegates to the dev postinstall scripts.
// In production (npm install from registry), copies the native binary from the
// platform-specific optionalDependency into bin/claude.exe.
//
// Platform detection + PLATFORMS map is duplicated in cli-wrapper.cjs — keep in sync.
const { spawnSync } = require('child_process')
const {
copyFileSync,
cpSync,
existsSync,
linkSync,
unlinkSync,
chmodSync,
readFileSync,
writeFileSync,
statSync,
readdirSync,
mkdirSync,
} = require('fs')
const { arch } = require('os')
const path = require('path')
// Dev environment detection: if .git exists at package root, we're in the monorepo
if (existsSync(path.join(__dirname, '.git'))) {
// densable Claude in Chrome uses @ant/claude-for-chrome-mcp + native host
// (CLI --chrome / /chrome). Do not run hangye mcp-chrome-bridge setup.
const r = spawnSync(
'node',
['scripts/run-parallel.mjs', 'scripts/postinstall.cjs'],
{ cwd: __dirname, stdio: 'inherit' },
)
process.exit(r.status ?? 0)
}
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,
},
'linux-arm64-android': {
pkg: PACKAGE_PREFIX + '-linux-arm64-android',
bin: BINARY_NAME,
},
'linux-x64-android': {
pkg: PACKAGE_PREFIX + '-linux-x64-android',
bin: BINARY_NAME,
},
'freebsd-x64': { pkg: PACKAGE_PREFIX + '-freebsd-x64', bin: BINARY_NAME },
'freebsd-arm64': {
pkg: PACKAGE_PREFIX + '-freebsd-arm64',
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 === 'android') return 'linux-' + cpu + '-android'
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 placeBinary(src, dest) {
try {
linkSync(src, dest)
} catch (err) {
if (err.code === 'EEXIST') {
const stub = statSync(dest).size < 4096 ? readFileSync(dest) : null
unlinkSync(dest)
try {
linkSync(src, dest)
} catch {
try {
copyFileSync(src, dest)
} catch (copyErr) {
if (stub) {
try {
writeFileSync(dest, stub, { mode: 0o755 })
} catch {}
}
throw copyErr
}
}
} else if (err.code === 'EXDEV' || err.code === 'EPERM') {
copyFileSync(src, dest)
} else {
throw err
}
}
if (process.platform !== 'win32') chmodSync(dest, 0o755)
}
// npm install may drop the executable bit on vendored helpers (clipboard-image,
// ripgrep). Only the main binary was chmod'd before — restore +x under vendor/.
function ensureVendorBinariesExecutable(pkgDir) {
if (process.platform === 'win32') return
const vendorDir = path.join(pkgDir, 'vendor')
if (!existsSync(vendorDir)) return
const stack = [vendorDir]
while (stack.length > 0) {
const dir = stack.pop()
let entries
try {
entries = readdirSync(dir, { withFileTypes: true })
} catch {
continue
}
for (const entry of entries) {
const full = path.join(dir, entry.name)
if (entry.isDirectory()) {
stack.push(full)
continue
}
if (!entry.isFile()) continue
try {
chmodSync(full, 0o755)
} catch {
// Best-effort: missing write permission shouldn't fail install.
}
}
}
}
function main() {
const platformKey = getPlatformKey()
const info = PLATFORMS[platformKey]
if (!info) {
console.error(
`[${WRAPPER_NAME} postinstall] Unsupported platform: ${process.platform} ${arch()}`,
)
console.error(` Supported: ${Object.keys(PLATFORMS).join(', ')}`)
return
}
const optionalDeps = require('./package.json').optionalDependencies || {}
if (!optionalDeps[info.pkg]) {
console.error(
`[${WRAPPER_NAME} postinstall] Native binaries for ${platformKey} are not available on this release channel.`,
)
console.error(
` Available: ${Object.keys(optionalDeps)
.map(p => p.replace(PACKAGE_PREFIX + '-', ''))
.join(', ')}`,
)
return
}
let pkgDir
let src
try {
pkgDir = path.dirname(require.resolve(info.pkg + '/package.json'))
src = path.join(pkgDir, info.bin)
} catch {
console.error(
`[${WRAPPER_NAME} postinstall] Native package "${info.pkg}" not found.`,
)
console.error(
' This happens with --omit=optional or when the download failed.',
)
console.error(
' The `claude` command will print instructions when invoked.',
)
console.error(' Fallback: node ' + path.join(__dirname, 'cli-wrapper.cjs'))
return
}
const dest = path.join(__dirname, 'bin', 'claude.exe')
try {
placeBinary(src, dest)
// Bundled claude.exe resolves vendored rg next to process.execPath
// (bin/vendor/ripgrep/<platform>/rg[.exe]). Copy platform-package vendor
// tree beside the placed binary so Grep/Glob work without system rg.
copyVendorBesideBinary(pkgDir, dest)
ensureVendorBinariesExecutable(pkgDir)
ensureVendorBinariesExecutable(path.dirname(dest))
} catch (err) {
console.error(
`[${WRAPPER_NAME} postinstall] Failed to place binary: ${err.message}`,
)
console.error(' Fallback: node ' + path.join(__dirname, 'cli-wrapper.cjs'))
process.exitCode = 1
}
}
/**
* Copy platform package `vendor/` next to the installed native binary.
* Required on Windows (and useful elsewhere) so builtin ripgrep is found at:
* <dir of claude.exe>/vendor/ripgrep/<arch>-win32/rg.exe
*/
function copyVendorBesideBinary(pkgDir, destBinary) {
const srcVendor = path.join(pkgDir, 'vendor')
if (!existsSync(srcVendor)) return
const destVendor = path.join(path.dirname(destBinary), 'vendor')
try {
mkdirSync(path.dirname(destVendor), { recursive: true })
cpSync(srcVendor, destVendor, { recursive: true, force: true })
} catch (err) {
console.error(
`[${WRAPPER_NAME} postinstall] Warning: could not copy vendor helpers: ${err.message}`,
)
console.error(' Grep/Glob may fall back to system ripgrep if available.')
}
}
main()