-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
176 lines (154 loc) · 6.79 KB
/
Copy pathbuild.js
File metadata and controls
176 lines (154 loc) · 6.79 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
import { existsSync, mkdirSync, readFileSync, statSync, rmSync } from "fs";
import { join, extname } from "path";
import { formatBytes, formatDuration } from "./build/common.js";
import { buildWindowsInstaller, patchWindowsSubsystem } from "./build/windows.js";
import { buildMacBundle } from "./build/macos.js";
import { buildLinuxAppDir } from "./build/linux.js";
const CHANNELS = ["dev", "alpha", "beta", "release"];
const channel = process.argv[2];
if (!CHANNELS.includes(channel)) {
console.error(`Usage: bun run build.js <channel>\nChannels: ${CHANNELS.join(", ")}`);
process.exit(1);
}
const configPath = join(process.cwd(), "buntopconfig.json");
if (!existsSync(configPath)) {
console.error(`buntopconfig.json not found in ${process.cwd()}`);
process.exit(1);
}
const config = JSON.parse(readFileSync(configPath, "utf-8"));
const channelConfig = {
...config.channels?.default,
...config.channels?.[channel],
};
const name = config.name || "app";
const entry = config.entry || "index.js";
const outDir = join(process.cwd(), config.outDir || "dist");
const targets = channelConfig.targets ?? ["current"];
console.log(`> building "${name}" (${channel}) -> ${targets.join(", ")}`);
console.log(`> entry: ${entry}`);
console.log(`> outDir: ${outDir}`);
const buildStart = performance.now();
const buildBinDir = join(process.cwd(), config.buildBin || "build-bin");
console.log(`> cleaning ${outDir}`);
rmSync(outDir, { recursive: true, force: true });
mkdirSync(outDir, { recursive: true });
mkdirSync(buildBinDir, { recursive: true });
let installerLicense = config.license ? join(process.cwd(), config.license) : undefined;
if (installerLicense && !existsSync(installerLicense)) {
console.warn(`> license not found, skipping installer license page: ${installerLicense}`);
installerLicense = undefined;
}
let windowsIcon = config.icon;
let macIcon, linuxIcon;
if (config.icon && !existsSync(config.icon)) {
console.warn(`> icon not found, skipping icon generation: ${config.icon}`);
windowsIcon = undefined;
} else if (config.icon && extname(config.icon).toLowerCase() === ".png") {
const { default: generateIcon } = await import("icon-gen");
const iconsDir = join(outDir, "icons");
mkdirSync(iconsDir, { recursive: true });
console.log(`> generating icons from ${config.icon}`);
await generateIcon(config.icon, iconsDir, {
ico: { name: "icon" },
icns: { name: "icon" },
favicon: {
name: "icon",
pngSizes: [16, 24, 32, 48, 64, 128, 256, 512, 1024],
icoSizes: [16, 24, 32, 48, 64],
},
report: false,
});
windowsIcon = join(iconsDir, "icon.ico");
macIcon = join(iconsDir, "icon.icns");
linuxIcon = join(iconsDir, "icon256.png");
}
const buildInstaller = channelConfig.installer ?? (channel === "beta" || channel === "release");
const title = config.title || name;
const identifier =
config.identifier ||
`com.${(config.publisher || "buntop").toLowerCase().replace(/[^a-z0-9]+/g, "")}.${name}`;
const results = [];
for (const [index, target] of targets.entries()) {
const args = ["build", "--compile"];
if (channelConfig.minify) args.push("--minify");
if (channelConfig.sourcemap) args.push("--sourcemap");
if (channelConfig.bytecode) args.push("--bytecode");
args.push(`--define:__BUNTOP_CHANNEL__="${channel}"`);
args.push(`--define:__BUNTOP_IDENTIFIER__="${identifier}"`);
if (target !== "current") args.push(`--target=${target}`);
const isWindows =
target.includes("windows") || (target === "current" && process.platform === "win32");
if (isWindows) {
args.push("--windows-hide-console");
if (windowsIcon) args.push(`--windows-icon=${windowsIcon}`);
args.push(`--windows-title=${title}`);
args.push(`--windows-description=${config.description || title}`);
if (config.publisher) args.push(`--windows-publisher=${config.publisher}`);
if (config.version) args.push(`--windows-version=${config.version}`);
if (config.copyright) args.push(`--windows-copyright=${config.copyright}`);
}
const ext = target.includes("windows") ? ".exe" : "";
const suffix = target === "current" ? "" : `-${target.replace("bun-", "")}`;
const outfile = join(outDir, `${name}-${channel}${suffix}${ext}`);
args.push(entry, "--outfile", outfile);
console.log(`\n> [${index + 1}/${targets.length}] target: ${target}`);
console.log(`> bun ${args.join(" ")}`);
const targetStart = performance.now();
const proc = Bun.spawnSync(["bun", ...args], {
stdout: "inherit",
stderr: "inherit",
cwd: process.cwd(),
});
const elapsed = performance.now() - targetStart;
if (proc.exitCode !== 0) {
console.error(`> [${index + 1}/${targets.length}] ${target} failed after ${formatDuration(elapsed)}`);
process.exit(proc.exitCode);
}
const isWindowsBuild = isWindows && !ext;
const resolvedOutfile = isWindowsBuild ? `${outfile}.exe` : outfile;
if (isWindows) patchWindowsSubsystem(resolvedOutfile);
const size = existsSync(resolvedOutfile) ? statSync(resolvedOutfile).size : 0;
console.log(
`> [${index + 1}/${targets.length}] ${target} done in ${formatDuration(elapsed)} (${formatBytes(size)})`,
);
results.push({ target, outfile: resolvedOutfile, size, elapsed });
if (buildInstaller) {
const isMac = target.includes("darwin") || (target === "current" && process.platform === "darwin");
const isLinux = target.includes("linux") || (target === "current" && process.platform === "linux");
const ctx = {
outDir,
name,
channel,
target,
suffix,
exePath: resolvedOutfile,
title,
version: config.version,
description: config.description,
publisher: config.publisher,
copyright: config.copyright,
license: installerLicense,
identifier,
buildBinDir,
};
if (isWindows) {
const installerOut = await buildWindowsInstaller({ ...ctx, icon: windowsIcon });
if (installerOut)
results.push({ target: `${target} (installer)`, outfile: installerOut, size: statSync(installerOut).size, elapsed: 0 });
} else if (isMac) {
const { appDir, dmgOut } = buildMacBundle({ ...ctx, icon: macIcon });
results.push({ target: `${target} (bundle)`, outfile: appDir, size: 0, elapsed: 0 });
if (dmgOut)
results.push({ target: `${target} (installer)`, outfile: dmgOut, size: statSync(dmgOut).size, elapsed: 0 });
} else if (isLinux) {
const { appDir, tarOut } = await buildLinuxAppDir({ ...ctx, icon: linuxIcon });
results.push({ target: `${target} (AppDir)`, outfile: appDir, size: 0, elapsed: 0 });
results.push({ target: `${target} (installer)`, outfile: tarOut, size: statSync(tarOut).size, elapsed: 0 });
}
}
}
const totalElapsed = performance.now() - buildStart;
console.log(`\n> build complete in ${formatDuration(totalElapsed)}`);
for (const r of results) {
console.log(` - ${r.outfile} (${formatBytes(r.size)})`);
}