@hanzo/dev 0.6.66

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.
package/postinstall.js ADDED
@@ -0,0 +1,786 @@
1
+ #!/usr/bin/env node
2
+ // Non-functional change to trigger release workflow
3
+
4
+ import { existsSync, mkdirSync, createWriteStream, chmodSync, readFileSync, readSync, writeFileSync, unlinkSync, statSync, openSync, closeSync, copyFileSync, fsyncSync, renameSync, realpathSync } from 'fs';
5
+ import { join, dirname, resolve } from 'path';
6
+ import { fileURLToPath } from 'url';
7
+ import { get } from 'https';
8
+ import { platform, arch, tmpdir } from 'os';
9
+ import { execSync } from 'child_process';
10
+ import { createRequire } from 'module';
11
+
12
+ const __dirname = dirname(fileURLToPath(import.meta.url));
13
+
14
+ // Map Node.js platform/arch to Rust target triples
15
+ function getTargetTriple() {
16
+ const platformMap = {
17
+ 'darwin': 'apple-darwin',
18
+ 'linux': 'unknown-linux-musl', // Default to musl for better compatibility
19
+ 'win32': 'pc-windows-msvc'
20
+ };
21
+
22
+ const archMap = {
23
+ 'x64': 'x86_64',
24
+ 'arm64': 'aarch64'
25
+ };
26
+
27
+ const rustArch = archMap[arch()] || arch();
28
+ const rustPlatform = platformMap[platform()] || platform();
29
+
30
+ return `${rustArch}-${rustPlatform}`;
31
+ }
32
+
33
+ // Resolve a persistent user cache directory for binaries so that repeated
34
+ // npx installs can reuse a previously downloaded artifact and skip work.
35
+ function getCacheDir(version) {
36
+ const plt = platform();
37
+ const home = process.env.HOME || process.env.USERPROFILE || '';
38
+ let base = '';
39
+ if (plt === 'win32') {
40
+ base = process.env.LOCALAPPDATA || join(home, 'AppData', 'Local');
41
+ } else if (plt === 'darwin') {
42
+ base = join(home, 'Library', 'Caches');
43
+ } else {
44
+ base = process.env.XDG_CACHE_HOME || join(home, '.cache');
45
+ }
46
+ const dir = join(base, 'hanzo', 'dev', version);
47
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
48
+ return dir;
49
+ }
50
+
51
+ function getCachedBinaryPath(version, targetTriple, isWindows) {
52
+ const ext = isWindows ? '.exe' : '';
53
+ const cacheDir = getCacheDir(version);
54
+ return join(cacheDir, `code-${targetTriple}${ext}`);
55
+ }
56
+
57
+ const CODE_SHIM_SIGNATURES = [
58
+ '@hanzo/dev',
59
+ 'bin/coder.js',
60
+ '$(dirname "$0")/coder',
61
+ '%~dp0coder'
62
+ ];
63
+
64
+ function shimContentsLookOurs(contents) {
65
+ return CODE_SHIM_SIGNATURES.some(sig => contents.includes(sig));
66
+ }
67
+
68
+ function looksLikeOurCodeShim(path) {
69
+ try {
70
+ const contents = readFileSync(path, 'utf8');
71
+ return shimContentsLookOurs(contents);
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
76
+
77
+ function isWSL() {
78
+ if (platform() !== 'linux') return false;
79
+ try {
80
+ const ver = readFileSync('/proc/version', 'utf8').toLowerCase();
81
+ return ver.includes('microsoft') || !!process.env.WSL_DISTRO_NAME;
82
+ } catch { return false; }
83
+ }
84
+
85
+ function isPathOnWindowsFs(p) {
86
+ try {
87
+ const mounts = readFileSync('/proc/mounts', 'utf8').split(/\n/).filter(Boolean);
88
+ let best = { mount: '/', type: 'unknown', len: 1 };
89
+ for (const line of mounts) {
90
+ const parts = line.split(' ');
91
+ if (parts.length < 3) continue;
92
+ const mnt = parts[1];
93
+ const typ = parts[2];
94
+ if (p.startsWith(mnt) && mnt.length > best.len) best = { mount: mnt, type: typ, len: mnt.length };
95
+ }
96
+ return best.type === 'drvfs' || best.type === 'cifs';
97
+ } catch { return false; }
98
+ }
99
+
100
+ async function writeCacheAtomic(srcPath, cachePath) {
101
+ try {
102
+ if (existsSync(cachePath)) {
103
+ const ok = validateDownloadedBinary(cachePath).ok;
104
+ if (ok) return;
105
+ }
106
+ } catch {}
107
+ const dir = dirname(cachePath);
108
+ if (!existsSync(dir)) { try { mkdirSync(dir, { recursive: true }); } catch {} }
109
+ const tmp = cachePath + '.tmp-' + Math.random().toString(36).slice(2, 8);
110
+ copyFileSync(srcPath, tmp);
111
+ try { const fd = openSync(tmp, 'r'); try { fsyncSync(fd); } finally { closeSync(fd); } } catch {}
112
+ // Retry with exponential backoff up to ~1.6s total
113
+ const delays = [100, 200, 400, 800, 1200, 1600];
114
+ for (let i = 0; i < delays.length; i++) {
115
+ try {
116
+ if (existsSync(cachePath)) { try { unlinkSync(cachePath); } catch {} }
117
+ renameSync(tmp, cachePath);
118
+ return;
119
+ } catch {
120
+ await new Promise(r => setTimeout(r, delays[i]));
121
+ }
122
+ }
123
+ if (existsSync(cachePath)) { try { unlinkSync(cachePath); } catch {} }
124
+ renameSync(tmp, cachePath);
125
+ }
126
+
127
+ function resolveGlobalBinDir() {
128
+ const plt = platform();
129
+ const userAgent = process.env.npm_config_user_agent || '';
130
+
131
+ const fromPrefix = (prefixPath) => {
132
+ if (!prefixPath) return '';
133
+ return plt === 'win32' ? prefixPath : join(prefixPath, 'bin');
134
+ };
135
+
136
+ const prefixEnv = process.env.npm_config_prefix || process.env.PREFIX || '';
137
+ const direct = fromPrefix(prefixEnv);
138
+ if (direct) return direct;
139
+
140
+ const tryExec = (command) => {
141
+ try {
142
+ return execSync(command, {
143
+ stdio: ['ignore', 'pipe', 'ignore'],
144
+ shell: true,
145
+ }).toString().trim();
146
+ } catch {
147
+ return '';
148
+ }
149
+ };
150
+
151
+ const prefixFromNpm = fromPrefix(tryExec('npm prefix -g'));
152
+ if (prefixFromNpm) return prefixFromNpm;
153
+
154
+ const binFromNpm = tryExec('npm bin -g');
155
+ if (binFromNpm) return binFromNpm;
156
+
157
+ if (userAgent.includes('pnpm')) {
158
+ const pnpmBin = tryExec('pnpm bin --global');
159
+ if (pnpmBin) return pnpmBin;
160
+ const pnpmPrefix = fromPrefix(tryExec('pnpm env get prefix'));
161
+ if (pnpmPrefix) return pnpmPrefix;
162
+ }
163
+
164
+ if (userAgent.includes('yarn')) {
165
+ const yarnBin = tryExec('yarn global bin');
166
+ if (yarnBin) return yarnBin;
167
+ }
168
+
169
+ return '';
170
+ }
171
+
172
+ async function downloadBinary(url, dest, maxRedirects = 5, maxRetries = 3) {
173
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
174
+
175
+ const doAttempt = () => new Promise((resolve, reject) => {
176
+ const attempt = (currentUrl, redirectsLeft) => {
177
+ const req = get(currentUrl, (response) => {
178
+ const status = response.statusCode || 0;
179
+ const location = response.headers.location;
180
+
181
+ if ((status === 301 || status === 302 || status === 303 || status === 307 || status === 308) && location) {
182
+ if (redirectsLeft <= 0) {
183
+ reject(new Error(`Too many redirects while downloading ${currentUrl}`));
184
+ return;
185
+ }
186
+ attempt(location, redirectsLeft - 1);
187
+ return;
188
+ }
189
+
190
+ if (status === 200) {
191
+ const expected = parseInt(response.headers['content-length'] || '0', 10) || 0;
192
+ let bytes = 0;
193
+ let timer;
194
+ const timeoutMs = 30000; // 30s inactivity timeout
195
+
196
+ const resetTimer = () => {
197
+ if (timer) clearTimeout(timer);
198
+ timer = setTimeout(() => {
199
+ req.destroy(new Error('download stalled'));
200
+ }, timeoutMs);
201
+ };
202
+
203
+ resetTimer();
204
+ response.on('data', (chunk) => {
205
+ bytes += chunk.length;
206
+ resetTimer();
207
+ });
208
+
209
+ const file = createWriteStream(dest);
210
+ response.pipe(file);
211
+ file.on('finish', () => {
212
+ if (timer) clearTimeout(timer);
213
+ file.close();
214
+ if (expected && bytes !== expected) {
215
+ try { unlinkSync(dest); } catch {}
216
+ reject(new Error(`incomplete download: got ${bytes} of ${expected} bytes`));
217
+ } else if (bytes === 0) {
218
+ try { unlinkSync(dest); } catch {}
219
+ reject(new Error('empty download'));
220
+ } else {
221
+ resolve();
222
+ }
223
+ });
224
+ file.on('error', (err) => {
225
+ if (timer) clearTimeout(timer);
226
+ try { unlinkSync(dest); } catch {}
227
+ reject(err);
228
+ });
229
+ } else {
230
+ reject(new Error(`Failed to download: HTTP ${status}`));
231
+ }
232
+ });
233
+
234
+ req.on('error', (err) => {
235
+ try { unlinkSync(dest); } catch {}
236
+ reject(err);
237
+ });
238
+
239
+ // Absolute request timeout to avoid hanging forever
240
+ req.setTimeout(120000, () => {
241
+ req.destroy(new Error('download timed out'));
242
+ });
243
+ };
244
+
245
+ attempt(url, maxRedirects);
246
+ });
247
+
248
+ let attemptNum = 0;
249
+ while (true) {
250
+ try {
251
+ return await doAttempt();
252
+ } catch (e) {
253
+ attemptNum += 1;
254
+ if (attemptNum > maxRetries) throw e;
255
+ const backoff = Math.min(2000, 200 * attemptNum);
256
+ await sleep(backoff);
257
+ }
258
+ }
259
+ }
260
+
261
+ function validateDownloadedBinary(p) {
262
+ try {
263
+ const st = statSync(p);
264
+ if (!st.isFile() || st.size === 0) {
265
+ return { ok: false, reason: 'empty or not a regular file' };
266
+ }
267
+ const fd = openSync(p, 'r');
268
+ try {
269
+ const buf = Buffer.alloc(4);
270
+ const n = readSync(fd, buf, 0, 4, 0);
271
+ if (n < 2) return { ok: false, reason: 'too short' };
272
+ const plt = platform();
273
+ if (plt === 'win32') {
274
+ if (!(buf[0] === 0x4d && buf[1] === 0x5a)) return { ok: false, reason: 'invalid PE header (missing MZ)' };
275
+ } else if (plt === 'linux' || plt === 'android') {
276
+ if (!(buf[0] === 0x7f && buf[1] === 0x45 && buf[2] === 0x4c && buf[3] === 0x46)) return { ok: false, reason: 'invalid ELF header' };
277
+ } else if (plt === 'darwin') {
278
+ const isMachO = (buf[0] === 0xcf && buf[1] === 0xfa && buf[2] === 0xed && buf[3] === 0xfe) ||
279
+ (buf[0] === 0xca && buf[1] === 0xfe && buf[2] === 0xba && buf[3] === 0xbe);
280
+ if (!isMachO) return { ok: false, reason: 'invalid Mach-O header' };
281
+ }
282
+ return { ok: true };
283
+ } finally {
284
+ closeSync(fd);
285
+ }
286
+ } catch (e) {
287
+ return { ok: false, reason: e.message };
288
+ }
289
+ }
290
+
291
+ export async function runPostinstall(options = {}) {
292
+ const { skipGlobalAlias = false, invokedByRuntime = false } = options;
293
+ if (process.env.CODE_POSTINSTALL_DRY_RUN === '1') {
294
+ return { skipped: true };
295
+ }
296
+
297
+ if (invokedByRuntime) {
298
+ process.env.CODE_RUNTIME_POSTINSTALL = process.env.CODE_RUNTIME_POSTINSTALL || '1';
299
+ }
300
+ // Detect potential PATH conflict with an existing `code` command (e.g., VS Code)
301
+ // Only relevant for global installs; skip for npx/local installs to keep postinstall fast.
302
+ const ua = process.env.npm_config_user_agent || '';
303
+ const isNpx = ua.includes('npx');
304
+ const isGlobal = process.env.npm_config_global === 'true';
305
+ if (!skipGlobalAlias && isGlobal && !isNpx) {
306
+ try {
307
+ const whichCmd = process.platform === 'win32' ? 'where code' : 'command -v code || which code || true';
308
+ const resolved = execSync(whichCmd, { stdio: ['ignore', 'pipe', 'ignore'], shell: process.platform !== 'win32' }).toString().split(/\r?\n/).filter(Boolean)[0];
309
+ if (resolved) {
310
+ let contents = '';
311
+ try {
312
+ contents = readFileSync(resolved, 'utf8');
313
+ } catch {
314
+ contents = '';
315
+ }
316
+ const looksLikeOurs = shimContentsLookOurs(contents);
317
+ if (!looksLikeOurs) {
318
+ console.warn('[notice] Found an existing `code` on PATH at:');
319
+ console.warn(` ${resolved}`);
320
+ console.warn('[notice] We will still install our CLI, also available as `coder`.');
321
+ console.warn(' If `code` runs another tool, prefer using: coder');
322
+ console.warn(' Or run our CLI explicitly via: npx -y @hanzo/dev');
323
+ }
324
+ }
325
+ } catch {
326
+ // Ignore detection failures; proceed with install.
327
+ }
328
+ }
329
+
330
+ const targetTriple = getTargetTriple();
331
+ const isWindows = platform() === 'win32';
332
+ const binaryExt = isWindows ? '.exe' : '';
333
+
334
+ const binDir = join(__dirname, 'bin');
335
+ if (!existsSync(binDir)) {
336
+ mkdirSync(binDir, { recursive: true });
337
+ }
338
+
339
+ // Get package version - use readFileSync for compatibility
340
+ const packageJson = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'));
341
+ const version = packageJson.version;
342
+
343
+ // Download only the primary binary; we'll create wrappers for legacy names.
344
+ const binaries = ['code'];
345
+
346
+ console.log(`Installing @hanzo/dev v${version} for ${targetTriple}...`);
347
+
348
+ for (const binary of binaries) {
349
+ const binaryName = `${binary}-${targetTriple}${binaryExt}`;
350
+ const localPath = join(binDir, binaryName);
351
+ const cachePath = getCachedBinaryPath(version, targetTriple, isWindows);
352
+
353
+ // On Windows we avoid placing the executable inside node_modules to prevent
354
+ // EBUSY/EPERM during global upgrades when the binary is in use.
355
+ // We treat the user cache path as the canonical home of the native binary.
356
+ // For macOS/Linux we keep previous behavior and also place a copy in binDir
357
+ // for convenience.
358
+
359
+ // Fast path: if a valid cached binary exists for this version+triple, reuse it.
360
+ try {
361
+ if (existsSync(cachePath)) {
362
+ const valid = validateDownloadedBinary(cachePath);
363
+ if (valid.ok) {
364
+ // Avoid mirroring into node_modules on Windows or WSL-on-NTFS.
365
+ const wsl = isWSL();
366
+ const binDirReal = (() => { try { return realpathSync(binDir); } catch { return binDir; } })();
367
+ const mirrorToLocal = !(isWindows || (wsl && isPathOnWindowsFs(binDirReal)));
368
+ if (mirrorToLocal) {
369
+ copyFileSync(cachePath, localPath);
370
+ try { chmodSync(localPath, 0o755); } catch {}
371
+ }
372
+ console.log(`✓ ${binaryName} ready from user cache`);
373
+ continue; // next binary
374
+ }
375
+ }
376
+ } catch {
377
+ // Ignore cache errors and fall through to normal paths
378
+ }
379
+
380
+ // First try platform package via npm optionalDependencies (fast path on npm CDN).
381
+ const require = createRequire(import.meta.url);
382
+ const platformPkg = (() => {
383
+ const name = (() => {
384
+ if (isWindows) return '@hanzo/dev-win32-x64';
385
+ const plt = platform();
386
+ const cpu = arch();
387
+ if (plt === 'darwin' && cpu === 'arm64') return '@hanzo/dev-darwin-arm64';
388
+ if (plt === 'darwin' && cpu === 'x64') return '@hanzo/dev-darwin-x64';
389
+ if (plt === 'linux' && cpu === 'x64') return '@hanzo/dev-linux-x64-musl';
390
+ if (plt === 'linux' && cpu === 'arm64') return '@hanzo/dev-linux-arm64-musl';
391
+ return null;
392
+ })();
393
+ if (!name) return null;
394
+ try {
395
+ const pkgJsonPath = require.resolve(`${name}/package.json`);
396
+ const pkgDir = dirname(pkgJsonPath);
397
+ return { name, dir: pkgDir };
398
+ } catch {
399
+ return null;
400
+ }
401
+ })();
402
+
403
+ if (platformPkg) {
404
+ try {
405
+ // Expect binary inside platform package bin directory
406
+ const src = join(platformPkg.dir, 'bin', binaryName);
407
+ if (!existsSync(src)) {
408
+ throw new Error(`platform package missing binary: ${platformPkg.name}`);
409
+ }
410
+ // Populate cache first (canonical location) atomically
411
+ await writeCacheAtomic(src, cachePath);
412
+ // Mirror into local bin only on Unix-like filesystems (not Windows/WSL-on-NTFS)
413
+ const wsl = isWSL();
414
+ const binDirReal = (() => { try { return realpathSync(binDir); } catch { return binDir; } })();
415
+ const mirrorToLocal = !(isWindows || (wsl && isPathOnWindowsFs(binDirReal)));
416
+ if (mirrorToLocal) {
417
+ copyFileSync(cachePath, localPath);
418
+ try { chmodSync(localPath, 0o755); } catch {}
419
+ }
420
+ console.log(`✓ Installed ${binaryName} from ${platformPkg.name} (cached)`);
421
+ continue; // next binary
422
+ } catch (e) {
423
+ console.warn(`⚠ Failed platform package install (${e.message}), falling back to GitHub download`);
424
+ }
425
+ }
426
+
427
+ // Decide archive format per OS with fallback on macOS/Linux:
428
+ // - Windows: .zip
429
+ // - macOS/Linux: prefer .zst if `zstd` CLI is available; otherwise use .tar.gz
430
+ const isWin = isWindows;
431
+ const detectedWSL = (() => {
432
+ if (platform() !== 'linux') return false;
433
+ try {
434
+ const ver = readFileSync('/proc/version', 'utf8').toLowerCase();
435
+ return ver.includes('microsoft') || !!process.env.WSL_DISTRO_NAME;
436
+ } catch { return false; }
437
+ })();
438
+ const binDirReal = (() => { try { return realpathSync(binDir); } catch { return binDir; } })();
439
+ const mirrorToLocal = !(isWin || (detectedWSL && isPathOnWindowsFs(binDirReal)));
440
+ let useZst = false;
441
+ if (!isWin) {
442
+ try {
443
+ execSync('zstd --version', { stdio: 'ignore', shell: true });
444
+ useZst = true;
445
+ } catch {
446
+ useZst = false;
447
+ }
448
+ }
449
+ const archiveName = isWin ? `${binaryName}.zip` : (useZst ? `${binaryName}.zst` : `${binaryName}.tar.gz`);
450
+ const downloadUrl = `https://github.com/hanzoai/dev/releases/download/v${version}/${archiveName}`;
451
+
452
+ console.log(`Downloading ${archiveName}...`);
453
+ try {
454
+ const needsIsolation = isWin || (!isWin && !mirrorToLocal); // Windows or WSL-on-NTFS
455
+ let safeTempDir = needsIsolation ? join(tmpdir(), 'hanzo', 'dev', version) : binDir;
456
+ // Ensure staging dir exists; if tmp fails (permissions/space), fall back to user cache.
457
+ if (needsIsolation) {
458
+ try {
459
+ if (!existsSync(safeTempDir)) mkdirSync(safeTempDir, { recursive: true });
460
+ } catch {
461
+ try {
462
+ safeTempDir = getCacheDir(version);
463
+ if (!existsSync(safeTempDir)) mkdirSync(safeTempDir, { recursive: true });
464
+ } catch {}
465
+ }
466
+ }
467
+ const tmpPath = join(needsIsolation ? safeTempDir : binDir, `.${archiveName}.part`);
468
+ await downloadBinary(downloadUrl, tmpPath);
469
+
470
+ if (isWin) {
471
+ // Unzip to a temp directory, then move into the per-user cache.
472
+ const unzipDest = safeTempDir;
473
+ try {
474
+ const sysRoot = process.env.SystemRoot || process.env.windir || 'C:\\Windows';
475
+ const psFull = join(sysRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
476
+ const psCmd = `Expand-Archive -Path '${tmpPath}' -DestinationPath '${unzipDest}' -Force`;
477
+ let ok = false;
478
+ // Attempt full-path powershell.exe
479
+ try { execSync(`"${psFull}" -NoProfile -NonInteractive -Command "${psCmd}"`, { stdio: 'ignore' }); ok = true; } catch {}
480
+ // Fallback to powershell in PATH
481
+ if (!ok) { try { execSync(`powershell -NoProfile -NonInteractive -Command "${psCmd}"`, { stdio: 'ignore' }); ok = true; } catch {} }
482
+ // Fallback to pwsh (PowerShell 7)
483
+ if (!ok) { try { execSync(`pwsh -NoProfile -NonInteractive -Command "${psCmd}"`, { stdio: 'ignore' }); ok = true; } catch {} }
484
+ // Final fallback: bsdtar can extract .zip
485
+ if (!ok) { execSync(`tar -xf "${tmpPath}" -C "${unzipDest}"`, { stdio: 'ignore', shell: true }); }
486
+ } catch (e) {
487
+ throw new Error(`failed to unzip archive: ${e.message}`);
488
+ } finally {
489
+ try { unlinkSync(tmpPath); } catch {}
490
+ }
491
+ // Move the extracted file from temp to cache; do not leave a copy in node_modules
492
+ try {
493
+ const extractedPath = join(unzipDest, binaryName);
494
+ await writeCacheAtomic(extractedPath, cachePath);
495
+ try { unlinkSync(extractedPath); } catch {}
496
+ } catch (e) {
497
+ throw new Error(`failed to move binary to cache: ${e.message}`);
498
+ }
499
+ } else {
500
+ if (useZst) {
501
+ // Decompress .zst via system zstd
502
+ try {
503
+ const outPath = mirrorToLocal ? localPath : join(safeTempDir, binaryName);
504
+ execSync(`zstd -d '${tmpPath}' -o '${outPath}'`, { stdio: 'ignore', shell: true });
505
+ } catch (e) {
506
+ try { unlinkSync(tmpPath); } catch {}
507
+ throw new Error(`failed to decompress .zst (need zstd CLI): ${e.message}`);
508
+ }
509
+ try { unlinkSync(tmpPath); } catch {}
510
+ } else {
511
+ // Extract .tar.gz using system tar
512
+ try {
513
+ const dest = mirrorToLocal ? binDir : safeTempDir;
514
+ execSync(`tar -xzf '${tmpPath}' -C '${dest}'`, { stdio: 'ignore', shell: true });
515
+ } catch (e) {
516
+ try { unlinkSync(tmpPath); } catch {}
517
+ throw new Error(`failed to extract .tar.gz: ${e.message}`);
518
+ }
519
+ try { unlinkSync(tmpPath); } catch {}
520
+ }
521
+ if (!mirrorToLocal) {
522
+ try {
523
+ const extractedPath = join(safeTempDir, binaryName);
524
+ await writeCacheAtomic(extractedPath, cachePath);
525
+ try { unlinkSync(extractedPath); } catch {}
526
+ } catch (e) {
527
+ throw new Error(`failed to move binary to cache: ${e.message}`);
528
+ }
529
+ }
530
+ }
531
+
532
+ // Validate header to avoid corrupt binaries causing spawn EFTYPE/ENOEXEC
533
+
534
+ const valid = validateDownloadedBinary(isWin ? cachePath : (mirrorToLocal ? localPath : cachePath));
535
+ if (!valid.ok) {
536
+ try { (isWin || !mirrorToLocal) ? unlinkSync(cachePath) : unlinkSync(localPath); } catch {}
537
+ throw new Error(`invalid binary (${valid.reason})`);
538
+ }
539
+
540
+ // Make executable on Unix-like systems
541
+ if (!isWin && mirrorToLocal) {
542
+ chmodSync(localPath, 0o755);
543
+ }
544
+
545
+ console.log(`✓ Installed ${binaryName}${(isWin || !mirrorToLocal) ? ' (cached)' : ''}`);
546
+ // Ensure persistent cache holds the binary (already true for Windows path)
547
+ if (!isWin && mirrorToLocal) {
548
+ try { await writeCacheAtomic(localPath, cachePath); } catch {}
549
+ }
550
+ } catch (error) {
551
+ console.error(`✗ Failed to install ${binaryName}: ${error.message}`);
552
+ console.error(` Downloaded from: ${downloadUrl}`);
553
+ // Continue with other binaries even if one fails
554
+ }
555
+ }
556
+
557
+ // Create platform-specific symlink/copy for main binary
558
+ const mainBinary = `code-${targetTriple}${binaryExt}`;
559
+ const mainBinaryPath = join(binDir, mainBinary);
560
+
561
+ if (existsSync(mainBinaryPath) || existsSync(getCachedBinaryPath(version, targetTriple, platform() === 'win32'))) {
562
+ try {
563
+ const probePath = existsSync(mainBinaryPath) ? mainBinaryPath : getCachedBinaryPath(version, targetTriple, platform() === 'win32');
564
+ const stats = statSync(probePath);
565
+ if (!stats.size) throw new Error('binary is empty (download likely failed)');
566
+ const valid = validateDownloadedBinary(probePath);
567
+ if (!valid.ok) {
568
+ console.warn(`⚠ Main code binary appears invalid: ${valid.reason}`);
569
+ console.warn(' Try reinstalling or check your network/proxy settings.');
570
+ }
571
+ } catch (e) {
572
+ console.warn(`⚠ Main code binary appears invalid: ${e.message}`);
573
+ console.warn(' Try reinstalling or check your network/proxy settings.');
574
+ }
575
+ console.log('Setting up main code binary...');
576
+
577
+ // On Windows, we can't use symlinks easily, so update the JS wrapper
578
+ // On Unix, the JS wrapper will find the correct binary
579
+ console.log('✓ Installation complete!');
580
+ } else {
581
+ console.warn('⚠ Main code binary not found. You may need to build from source.');
582
+ }
583
+
584
+ // Handle collisions (e.g., VS Code) and add wrappers. We no longer publish a
585
+ // `code` bin in package.json. Instead, for global installs we create a `code`
586
+ // wrapper only when there is no conflicting `code` earlier on PATH. This avoids
587
+ // hijacking the VS Code CLI while still giving users a friendly name when safe.
588
+ // For upgrades from older versions that published a `code` bin, we also remove
589
+ // our old shim if a conflict is detected.
590
+ if (isGlobal && !isNpx) try {
591
+ const isTTY = process.stdout && process.stdout.isTTY;
592
+ const isWindows = platform() === 'win32';
593
+ const ua = process.env.npm_config_user_agent || '';
594
+ const isBun = ua.includes('bun') || !!process.env.BUN_INSTALL;
595
+
596
+ const installedCmds = new Set(['coder']); // global install always exposes coder via package manager
597
+ const skippedCmds = [];
598
+
599
+ // Helper to resolve all 'code' on PATH
600
+ const resolveAllOnPath = () => {
601
+ try {
602
+ if (isWindows) {
603
+ const out = execSync('where code', { stdio: ['ignore', 'pipe', 'ignore'] }).toString();
604
+ return out.split(/\r?\n/).map(s => s.trim()).filter(Boolean);
605
+ }
606
+ let out = '';
607
+ try {
608
+ out = execSync('bash -lc "which -a code 2>/dev/null"', { stdio: ['ignore', 'pipe', 'ignore'] }).toString();
609
+ } catch {
610
+ try {
611
+ out = execSync('command -v code || true', { stdio: ['ignore', 'pipe', 'ignore'] }).toString();
612
+ } catch { out = ''; }
613
+ }
614
+ return out.split(/\r?\n/).map(s => s.trim()).filter(Boolean);
615
+ } catch {
616
+ return [];
617
+ }
618
+ };
619
+
620
+ if (isBun) {
621
+ // Bun creates shims for every bin; if another 'code' exists elsewhere on PATH, remove Bun's shim
622
+ let bunBin = '';
623
+ try {
624
+ const home = process.env.HOME || process.env.USERPROFILE || '';
625
+ const bunBase = process.env.BUN_INSTALL || join(home, '.bun');
626
+ bunBin = join(bunBase, 'bin');
627
+ } catch {}
628
+
629
+ const bunShim = join(bunBin || '', isWindows ? 'code.cmd' : 'code');
630
+ const candidates = resolveAllOnPath();
631
+ const other = candidates.find(p => p && (!bunBin || !p.startsWith(bunBin)));
632
+ if (other && existsSync(bunShim)) {
633
+ try {
634
+ unlinkSync(bunShim);
635
+ console.log(`✓ Skipped global 'code' shim under Bun (existing: ${other})`);
636
+ skippedCmds.push({ name: 'code', reason: `existing: ${other}` });
637
+ } catch (e) {
638
+ console.log(`⚠ Could not remove Bun shim '${bunShim}': ${e.message}`);
639
+ }
640
+ } else if (!other) {
641
+ // No conflict: create a wrapper that forwards to `coder`
642
+ try {
643
+ const wrapperPath = bunShim;
644
+ if (isWindows) {
645
+ const content = `@echo off\r\n"%~dp0coder" %*\r\n`;
646
+ writeFileSync(wrapperPath, content);
647
+ } else {
648
+ const content = `#!/bin/sh\nexec "$(dirname \"$0\")/coder" "$@"\n`;
649
+ writeFileSync(wrapperPath, content);
650
+ chmodSync(wrapperPath, 0o755);
651
+ }
652
+ console.log("✓ Created 'code' wrapper -> coder (bun)");
653
+ installedCmds.add('code');
654
+ } catch (e) {
655
+ console.log(`⚠ Failed to create 'code' wrapper (bun): ${e.message}`);
656
+ }
657
+ }
658
+
659
+ // Print summary for Bun
660
+ const list = Array.from(installedCmds).sort().join(', ');
661
+ console.log(`Commands installed (bun): ${list}`);
662
+ if (skippedCmds.length) {
663
+ for (const s of skippedCmds) console.error(`Commands skipped: ${s.name} (${s.reason})`);
664
+ console.error('→ Use `coder` to run this tool.');
665
+ }
666
+ // Final friendly usage hint
667
+ if (installedCmds.has('code')) {
668
+ console.log("Use 'code' to launch Code.");
669
+ } else {
670
+ console.log("Use 'coder' to launch Code.");
671
+ }
672
+ } else {
673
+ // npm/pnpm/yarn path
674
+ const globalBin = resolveGlobalBinDir();
675
+ const ourShim = globalBin ? join(globalBin, isWindows ? 'code.cmd' : 'code') : '';
676
+ const candidates = resolveAllOnPath();
677
+ const others = candidates.filter(p => p && (!ourShim || p !== ourShim));
678
+ const ourShimExists = ourShim && existsSync(ourShim);
679
+ const shimLooksOurs = ourShimExists && looksLikeOurCodeShim(ourShim);
680
+ const conflictPaths = [
681
+ ...others,
682
+ ...(ourShimExists && !shimLooksOurs ? [ourShim] : []),
683
+ ];
684
+ const collision = conflictPaths.length > 0;
685
+
686
+ const ensureWrapper = (name, args) => {
687
+ if (!globalBin) return;
688
+ try {
689
+ const wrapperPath = join(globalBin, isWindows ? `${name}.cmd` : name);
690
+ if (isWindows) {
691
+ const content = `@echo off\r\n"%~dp0${collision ? 'coder' : 'code'}" ${args} %*\r\n`;
692
+ writeFileSync(wrapperPath, content);
693
+ } else {
694
+ const content = `#!/bin/sh\nexec "$(dirname \"$0\")/${collision ? 'coder' : 'code'}" ${args} "$@"\n`;
695
+ writeFileSync(wrapperPath, content);
696
+ chmodSync(wrapperPath, 0o755);
697
+ }
698
+ console.log(`✓ Created wrapper '${name}' -> ${collision ? 'coder' : 'code'} ${args}`);
699
+ installedCmds.add(name);
700
+ } catch (e) {
701
+ console.log(`⚠ Failed to create '${name}' wrapper: ${e.message}`);
702
+ }
703
+ };
704
+
705
+ // Always create legacy wrappers so existing scripts keep working
706
+ ensureWrapper('code-tui', '');
707
+ ensureWrapper('code-exec', 'exec');
708
+
709
+ if (collision) {
710
+ console.error('⚠ Detected existing `code` on PATH:');
711
+ for (const p of conflictPaths) console.error(` - ${p}`);
712
+ if (globalBin) {
713
+ try {
714
+ if (ourShimExists) {
715
+ if (shimLooksOurs && others.length > 0) {
716
+ unlinkSync(ourShim);
717
+ console.error(`✓ Removed global 'code' shim (ours) at ${ourShim}`);
718
+ const reason = others[0] || ourShim;
719
+ skippedCmds.push({ name: 'code', reason: `existing: ${reason}` });
720
+ } else if (!shimLooksOurs) {
721
+ console.error(`✓ Skipped global 'code' shim (different CLI at ${ourShim})`);
722
+ const reason = conflictPaths[0] || ourShim;
723
+ skippedCmds.push({ name: 'code', reason: `existing: ${reason}` });
724
+ }
725
+ } else {
726
+ const reason = conflictPaths[0] || 'another command on PATH';
727
+ skippedCmds.push({ name: 'code', reason: `existing: ${reason}` });
728
+ }
729
+ } catch (e) {
730
+ console.error(`⚠ Could not remove npm shim '${ourShim}': ${e.message}`);
731
+ }
732
+ console.error('→ Use `coder` to run this tool.');
733
+ } else {
734
+ console.log('Note: could not determine npm global bin; skipping alias creation.');
735
+ }
736
+ } else {
737
+ // No collision; ensure a 'code' wrapper exists forwarding to 'coder'
738
+ if (globalBin) {
739
+ try {
740
+ const content = isWindows
741
+ ? `@echo off\r\n"%~dp0coder" %*\r\n`
742
+ : `#!/bin/sh\nexec "$(dirname \"$0\")/coder" "$@"\n`;
743
+ writeFileSync(ourShim, content);
744
+ if (!isWindows) chmodSync(ourShim, 0o755);
745
+ console.log("✓ Created 'code' wrapper -> coder");
746
+ installedCmds.add('code');
747
+ } catch (e) {
748
+ console.log(`⚠ Failed to create 'code' wrapper: ${e.message}`);
749
+ }
750
+ }
751
+ }
752
+
753
+ // Print summary for npm/pnpm/yarn
754
+ const list = Array.from(installedCmds).sort().join(', ');
755
+ console.log(`Commands installed: ${list}`);
756
+ if (skippedCmds.length) {
757
+ for (const s of skippedCmds) console.log(`Commands skipped: ${s.name} (${s.reason})`);
758
+ }
759
+ // Final friendly usage hint
760
+ if (installedCmds.has('code')) {
761
+ console.log("Use 'code' to launch Code.");
762
+ } else {
763
+ console.log("Use 'coder' to launch Code.");
764
+ }
765
+ }
766
+ } catch {
767
+ // non-fatal
768
+ }
769
+ }
770
+
771
+ function isExecutedDirectly() {
772
+ const entry = process.argv[1];
773
+ if (!entry) return false;
774
+ try {
775
+ return resolve(entry) === fileURLToPath(import.meta.url);
776
+ } catch {
777
+ return false;
778
+ }
779
+ }
780
+
781
+ if (isExecutedDirectly()) {
782
+ runPostinstall().catch(error => {
783
+ console.error('Installation failed:', error);
784
+ process.exit(1);
785
+ });
786
+ }