-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
121 lines (103 loc) · 2.88 KB
/
build.ts
File metadata and controls
121 lines (103 loc) · 2.88 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
import z from "zod";
import { arch, platform } from "os";
import { resolve, basename } from "path";
import { pack } from "7zip-min";
import { mkdir, rm } from "fs-extra";
import { execa } from "execa";
// https://bun.sh/docs/bundler/executables
const platforms = z.enum([
"linux-x64",
"linux-arm64",
"windows-x64",
"darwin-x64",
"darwin-arm64",
"linux-x64-musl",
"linux-arm64-musl",
]);
const allPlatforms = z.array(platforms).or(z.literal("all")).optional();
const inputType = z.strictObject({
inputFile: z.string(),
target: allPlatforms,
name: z.string().optional(),
hideConsole: z.boolean().optional(),
icon: z.string().optional(),
bytecode: z.boolean().optional(),
compress: z.boolean().optional(),
outputDir: z.string().optional(),
});
type Input = z.infer<typeof inputType>;
export const vs = Object.values(platforms.Values);
export const compile = async (params: Input) => {
// 检测是否有 bun 命令
try {
await execa("bun", ["--version"]);
} catch (error) {
throw new Error("Bun not found, please install it first.");
}
const {
target,
outputDir: output,
inputFile: input,
compress,
hideConsole,
icon,
name,
bytecode,
} = inputType.parse(params);
// 获取当前系统和架构
const currentPlatform = platform();
const currentArch = arch();
// 构建目标平台和架构的字符串
const targetPlatformArch = target ?? [
`${currentPlatform.replace("win32", "windows")}-${currentArch}`,
];
// 目标平台和架构
const tgs =
targetPlatformArch === "all"
? vs
: Array.from(new Set(targetPlatformArch).values());
// 输出文件夹路径
const o = resolve(output ?? "compile");
// 获取文件名
const fileName = name ?? "main";
// 输出文件名
const outs = tgs.map((v) => resolve(o, `${fileName}-${v}`));
// 打包命令
const cmds = tgs.map((v, index) => {
const cmd =
`bun build` +
`${bytecode ? ` --bytecode ` : " "}` +
`--compile --minify --sourcemap --target=bun-${v} --outfile "${outs[index]}" ${input}`;
return {
outFile: outs[index],
platform: v,
cmd,
};
});
// 删除并创建目录
await rm(o, { force: true, recursive: true });
await mkdir(o, { recursive: true });
// 并发执行命令
await Promise.all(
cmds.map(async (v) => {
await execa(v.cmd, { shell: true });
if (compress && v.outFile) {
const source = `${v.outFile}${
v.platform.startsWith("windows") ? ".exe" : ""
}`;
const targetDir = resolve(o, "compressed");
await mkdir(targetDir, { recursive: true });
const target = resolve(targetDir, `${basename(v.outFile)}.7z`);
// 压缩文件
await pack(source, target);
}
})
);
};
await compile({
inputFile: "src/index.ts",
target: "all",
name: "DocBase",
hideConsole: true,
compress: true,
});