-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
120 lines (103 loc) · 4.49 KB
/
build.ts
File metadata and controls
120 lines (103 loc) · 4.49 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
#!/usr/bin/env bun
/**
* Build script – compiles github-issue-ops.ts into a standalone binary.
*
* Usage:
* bun run build.ts # current platform
* bun run build.ts --target=bun-linux-x64 # cross-compile
*
* Supported targets (Bun executables):
* bun-linux-x64 bun-linux-x64-baseline bun-linux-arm64
* bun-darwin-x64 bun-darwin-arm64
* bun-windows-x64
*/
import { version } from "./package.json" with { type: "json" };
// ─── CLI args ──────────────────────────────────────────────────────────────────
const targetArg = process.argv.find((a) => a.startsWith("--target="));
const target = targetArg?.slice("--target=".length) ?? null;
// ─── Derive OS / arch from target ─────────────────────────────────────────────
function parseTarget(t: string): { os: string; arch: string } {
const s = t.replace(/^bun-/, "");
if (s.startsWith("linux-x64-baseline")) return { os: "linux", arch: "x64-baseline" };
if (s.startsWith("linux-x64")) return { os: "linux", arch: "x64" };
if (s.startsWith("linux-arm64-musl")) return { os: "linux", arch: "arm64-musl" };
if (s.startsWith("linux-arm64")) return { os: "linux", arch: "arm64" };
if (s.startsWith("darwin-x64")) return { os: "darwin", arch: "x64" };
if (s.startsWith("darwin-arm64")) return { os: "darwin", arch: "arm64" };
if (s.startsWith("windows-x64")) return { os: "windows", arch: "x64" };
return { os: process.platform, arch: process.arch };
}
const { os: targetOs, arch: targetArch } = target
? parseTarget(target)
: {
os: process.platform,
arch: process.arch === "x64" ? "x64" : process.arch,
};
// ─── Output path ───────────────────────────────────────────────────────────────
const ext = targetOs === "windows" ? ".exe" : "";
const suffix = target ? `-${target.replace(/^bun-/, "")}` : "";
const outfile = `./dist/github-issue-ops${suffix}${ext}`;
// ─── Git commit hash ──────────────────────────────────────────────────────────
let commit = "dev";
try {
const proc = Bun.spawn(["git", "rev-parse", "--short", "HEAD"], {
stdout: "pipe",
stderr: "pipe",
});
await proc.exited;
commit = (await new Response(proc.stdout).text()).trim() || "dev";
} catch {
// Not a git repo or git not available
}
// ─── Build ────────────────────────────────────────────────────────────────────
const label = `${version} (${commit} · ${targetOs}/${targetArch})`;
console.log(`Building github-issue-ops v${label}… outfile=${outfile}`);
if (target) console.log(` Target: ${target}`);
await Bun.$`mkdir -p dist`;
await Bun.build({
entrypoints: ["./github-issue-ops.ts"],
minify: true,
bytecode: true,
compile: {
outfile,
},
define: {
BUILD_VERSION: JSON.stringify(version),
BUILD_COMMIT: JSON.stringify(commit),
BUILD_TARGET_OS: JSON.stringify(targetOs),
BUILD_TARGET_ARCH: JSON.stringify(targetArch),
},
target: target ? target : undefined,
});
console.log(` Built ${outfile}`);
// ─── Ad-hoc codesign (macOS only) ────────────────────────────────────────────
if (targetOs === "darwin" && process.platform === "darwin") {
const sign = Bun.spawn(
[
"codesign",
"--deep",
"--force",
"--sign",
"-",
"--entitlements",
`${import.meta.dir}/entitlements.plist`,
outfile,
],
{ stdout: "inherit", stderr: "inherit" },
);
const signCode = await sign.exited;
if (signCode !== 0) {
console.error(`codesign failed (exit ${signCode})`);
process.exit(signCode);
}
console.log(` Codesigned ${outfile}`);
const verify = Bun.spawn(["codesign", "--verify", "--verbose", outfile], {
stdout: "inherit",
stderr: "inherit",
});
const verifyCode = await verify.exited;
if (verifyCode !== 0) {
console.error(`codesign verification failed (exit ${verifyCode})`);
process.exit(verifyCode);
}
}