|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -const { spawnSync } = require("node:child_process"); |
| 3 | +const childProcess = require("node:child_process"); |
| 4 | +const fs = require("node:fs"); |
| 5 | +const os = require("node:os"); |
4 | 6 | const path = require("node:path"); |
5 | 7 |
|
6 | | -const entrypoint = path.join(__dirname, "..", "dist", "npm", "main.js"); |
| 8 | +function run(target, args) { |
| 9 | + const result = childProcess.spawnSync(target, args, { |
| 10 | + stdio: "inherit", |
| 11 | + env: process.env, |
| 12 | + }); |
7 | 13 |
|
8 | | -let bunBinary; |
| 14 | + if (result.error) { |
| 15 | + console.error(result.error.message); |
| 16 | + process.exit(1); |
| 17 | + } |
| 18 | + |
| 19 | + process.exit(typeof result.status === "number" ? result.status : 1); |
| 20 | +} |
| 21 | + |
| 22 | +function hostCandidates() { |
| 23 | + const platformMap = { |
| 24 | + darwin: "darwin", |
| 25 | + linux: "linux", |
| 26 | + win32: "windows", |
| 27 | + }; |
| 28 | + const archMap = { |
| 29 | + x64: "x64", |
| 30 | + arm64: "arm64", |
| 31 | + }; |
| 32 | + |
| 33 | + const platform = platformMap[os.platform()] || os.platform(); |
| 34 | + const arch = archMap[os.arch()] || os.arch(); |
| 35 | + const binary = platform === "windows" ? "hunk.exe" : "hunk"; |
9 | 36 |
|
10 | | -try { |
11 | | - bunBinary = require.resolve("bun/bin/bun.exe"); |
12 | | -} catch (error) { |
13 | | - console.error( |
14 | | - "Failed to resolve the bundled Bun runtime. Try reinstalling hunkdiff.", |
15 | | - ); |
16 | | - if (error && error.message) { |
17 | | - console.error(error.message); |
| 37 | + if (platform === "darwin") { |
| 38 | + if (arch === "arm64") return [{ packageName: "hunkdiff-darwin-arm64", binary }]; |
| 39 | + if (arch === "x64") return [{ packageName: "hunkdiff-darwin-x64", binary }]; |
18 | 40 | } |
19 | | - process.exit(1); |
| 41 | + |
| 42 | + if (platform === "linux") { |
| 43 | + if (arch === "arm64") return [{ packageName: "hunkdiff-linux-arm64", binary }]; |
| 44 | + if (arch === "x64") return [{ packageName: "hunkdiff-linux-x64", binary }]; |
| 45 | + } |
| 46 | + |
| 47 | + return []; |
20 | 48 | } |
21 | 49 |
|
22 | | -const result = spawnSync(bunBinary, [entrypoint, ...process.argv.slice(2)], { |
23 | | - stdio: "inherit", |
24 | | - env: process.env, |
25 | | -}); |
| 50 | +function findInstalledBinary(startDir) { |
| 51 | + let current = startDir; |
| 52 | + |
| 53 | + for (;;) { |
| 54 | + const modulesDir = path.join(current, "node_modules"); |
| 55 | + if (fs.existsSync(modulesDir)) { |
| 56 | + for (const candidate of hostCandidates()) { |
| 57 | + const resolved = path.join(modulesDir, candidate.packageName, "bin", candidate.binary); |
| 58 | + if (fs.existsSync(resolved)) { |
| 59 | + return resolved; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + const parent = path.dirname(current); |
| 65 | + if (parent === current) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + current = parent; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function bundledBunRuntime() { |
| 73 | + try { |
| 74 | + return require.resolve("bun/bin/bun.exe"); |
| 75 | + } catch { |
| 76 | + return null; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +const overrideBinary = process.env.HUNK_BIN_PATH; |
| 81 | +if (overrideBinary) { |
| 82 | + run(overrideBinary, process.argv.slice(2)); |
| 83 | +} |
| 84 | + |
| 85 | +const scriptDir = path.dirname(fs.realpathSync(__filename)); |
| 86 | +const prebuiltBinary = findInstalledBinary(scriptDir); |
| 87 | +if (prebuiltBinary) { |
| 88 | + run(prebuiltBinary, process.argv.slice(2)); |
| 89 | +} |
26 | 90 |
|
27 | | -if (result.error) { |
28 | | - console.error(result.error.message); |
29 | | - process.exit(1); |
| 91 | +const bunBinary = bundledBunRuntime(); |
| 92 | +if (bunBinary) { |
| 93 | + const entrypoint = path.join(__dirname, "..", "dist", "npm", "main.js"); |
| 94 | + run(bunBinary, [entrypoint, ...process.argv.slice(2)]); |
30 | 95 | } |
31 | 96 |
|
32 | | -process.exit(typeof result.status === "number" ? result.status : 1); |
| 97 | +const printablePackages = hostCandidates().map((candidate) => `"${candidate.packageName}"`).join(" or "); |
| 98 | +console.error( |
| 99 | + printablePackages.length > 0 |
| 100 | + ? `Failed to locate a matching prebuilt Hunk binary. Try reinstalling hunkdiff or manually installing ${printablePackages}.` |
| 101 | + : `Unsupported platform for prebuilt Hunk binaries: ${os.platform()} ${os.arch()}`, |
| 102 | +); |
| 103 | +process.exit(1); |
0 commit comments