-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathhunk.cjs
More file actions
executable file
·103 lines (86 loc) · 2.73 KB
/
hunk.cjs
File metadata and controls
executable file
·103 lines (86 loc) · 2.73 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
const childProcess = require("node:child_process");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
function run(target, args) {
const result = childProcess.spawnSync(target, args, {
stdio: "inherit",
env: process.env,
});
if (result.error) {
console.error(result.error.message);
process.exit(1);
}
process.exit(typeof result.status === "number" ? result.status : 1);
}
function hostCandidates() {
const platformMap = {
darwin: "darwin",
linux: "linux",
win32: "windows",
};
const archMap = {
x64: "x64",
arm64: "arm64",
};
const platform = platformMap[os.platform()] || os.platform();
const arch = archMap[os.arch()] || os.arch();
const binary = platform === "windows" ? "hunk.exe" : "hunk";
if (platform === "darwin") {
if (arch === "arm64") return [{ packageName: "hunkdiff-darwin-arm64", binary }];
if (arch === "x64") return [{ packageName: "hunkdiff-darwin-x64", binary }];
}
if (platform === "linux") {
if (arch === "arm64") return [{ packageName: "hunkdiff-linux-arm64", binary }];
if (arch === "x64") return [{ packageName: "hunkdiff-linux-x64", binary }];
}
return [];
}
function findInstalledBinary(startDir) {
let current = startDir;
for (;;) {
const modulesDir = path.join(current, "node_modules");
if (fs.existsSync(modulesDir)) {
for (const candidate of hostCandidates()) {
const resolved = path.join(modulesDir, candidate.packageName, "bin", candidate.binary);
if (fs.existsSync(resolved)) {
return resolved;
}
}
}
const parent = path.dirname(current);
if (parent === current) {
return null;
}
current = parent;
}
}
function bundledBunRuntime() {
try {
return require.resolve("bun/bin/bun.exe");
} catch {
return null;
}
}
const overrideBinary = process.env.HUNK_BIN_PATH;
if (overrideBinary) {
run(overrideBinary, process.argv.slice(2));
}
const scriptDir = path.dirname(fs.realpathSync(__filename));
const prebuiltBinary = findInstalledBinary(scriptDir);
if (prebuiltBinary) {
run(prebuiltBinary, process.argv.slice(2));
}
const bunBinary = bundledBunRuntime();
if (bunBinary) {
const entrypoint = path.join(__dirname, "..", "dist", "npm", "main.js");
run(bunBinary, [entrypoint, ...process.argv.slice(2)]);
}
const printablePackages = hostCandidates().map((candidate) => `"${candidate.packageName}"`).join(" or ");
console.error(
printablePackages.length > 0
? `Failed to locate a matching prebuilt Hunk binary. Try reinstalling hunkdiff or manually installing ${printablePackages}.`
: `Unsupported platform for prebuilt Hunk binaries: ${os.platform()} ${os.arch()}`,
);
process.exit(1);