Skip to content

Commit 21fb7df

Browse files
committed
Improve build
Signed-off-by: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com>
1 parent 75924e2 commit 21fb7df

File tree

8 files changed

+95
-67
lines changed

8 files changed

+95
-67
lines changed

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
run: deno task deps
2626

2727
- name: build
28-
run: ./scripts/generate-builds.ts
28+
run: ./scripts/bundle.ts && ./scripts/embed.ts && ./scripts/build.ts
2929

3030
- name: release
3131
uses: ncipollo/release-action@v1

.gitignore

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
.bin/*
22
!.bin/git-hooks
3-
!.bin/.gitkeep
4-
!.bin/public-to-json.ts
5-
share
6-
lib
7-
include
83
data
9-
src/public.json
10-
*.dll
11-
.cache
124
.env
5+
embed.json

deno.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
"version": "4.5.3",
44
"exports": "./src/main.ts",
55
"tasks": {
6-
"run": "./scripts/public-to-json.ts && deno run --allow-env=ENV --allow-net=127.0.0.1 --cached-only src/main.ts",
7-
"compile": "./scripts/public-to-json.ts && deno compile --allow-env=ENV --allow-net=127.0.0.1 --unstable-ffi --include ./src/public.json --cached-only --env=.env -o ./.bin/denotag ./src/main.ts",
8-
"compile:x86_64-pc-windows-msvc": "./scripts/public-to-json.ts && deno compile --allow-env=ENV --allow-net=127.0.0.1 --unstable-ffi --include ./src/public.json --cached-only --env=.env -o ./.bin/denotag.exe --target=x86_64-pc-windows-msvc ./src/main.ts",
9-
"compile:x86_64-apple-darwin": "./scripts/public-to-json.ts && deno compile --allow-env=ENV --allow-net=127.0.0.1 --unstable-ffi --include ./src/public.json --cached-only --env=.env -o ./.bin/denotag --target=x86_64-apple-darwin ./src/main.ts",
10-
"compile:aarch64-apple-darwin": "./scripts/public-to-json.ts && deno compile --allow-env=ENV --allow-net=127.0.0.1 --unstable-ffi --include ./src/public.json --cached-only --env=.env -o ./.bin/denotag --target=aarch64-apple-darwin ./src/main.ts",
11-
"compile:x86_64-unknown-linux-gnu": "./scripts/public-to-json.ts && deno compile --allow-env=ENV --allow-net=127.0.0.1 --unstable-ffi --include ./src/public.json --cached-only --env=.env -o ./.bin/denotag --target=x86_64-unknown-linux-gnu ./src/main.ts",
12-
"compile:aarch64-unknown-linux-gnu": "./scripts/public-to-json.ts && deno compile --allow-env=ENV --allow-net=127.0.0.1 --unstable-ffi --include ./src/public.json --cached-only --env=.env -o ./.bin/denotag --target=aarch64-unknown-linux-gnu ./src/main.ts",
6+
"run": "./scripts/bundle.ts && ./scripts/embed.ts && deno run --allow-env=ENV --allow-net=127.0.0.1 --cached-only src/main.ts",
7+
"compile": "./scripts/bundle.ts && ./scripts/embed.ts && deno compile --allow-env=ENV --allow-net=127.0.0.1 --cached-only --env=.env -o ./.bin/denotag ./src/main.ts",
138
"deps:lock": "deno cache --frozen=false src/**/*.ts src/**/*.tsx",
149
"deps": "deno cache --reload src/**/*.ts src/**/*.tsx"
1510
},
@@ -18,12 +13,13 @@
1813
"jsx": "react-jsx"
1914
},
2015
"lint": {
16+
"exclude": ["embed.json"],
2117
"rules": {
2218
"include": ["verbatim-module-syntax"]
2319
}
2420
},
2521
"fmt": {
26-
"exclude": ["./src/public.json"]
22+
"exclude": ["embed.json"]
2723
},
2824
"imports": {
2925
"@mjackson/multipart-parser": "jsr:@mjackson/multipart-parser@0.6.2",

scripts/build.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env -S deno run -A --no-lock
2+
3+
import $ from "jsr:@david/dax@0.42.0";
4+
import { basename, resolve } from "@std/path";
5+
6+
const rootDir = resolve(import.meta.dirname!, "../");
7+
const binDir = resolve(rootDir, ".bin");
8+
const binName = "denotag" as const;
9+
10+
const targets = [
11+
"x86_64-pc-windows-msvc",
12+
"x86_64-apple-darwin",
13+
"aarch64-apple-darwin",
14+
"x86_64-unknown-linux-gnu",
15+
"aarch64-unknown-linux-gnu",
16+
] as const;
17+
18+
for (const file of Deno.readDirSync("./.bin")) {
19+
if (
20+
file.name.match(/.*\.(zip|tar\.gz)(\.sha256)?$/) ||
21+
targets.some((target) => file.name.includes(target))
22+
) {
23+
Deno.removeSync(resolve(binDir, file.name), { recursive: true });
24+
}
25+
}
26+
27+
const buildFor = async (target: typeof targets[number]) => {
28+
const finalBinDir = resolve(binDir, `${binName}-${target}`);
29+
const finalBinName = target.includes("windows") ? `${binName}.exe` : binName;
30+
const finalCompressName = target.includes("windows")
31+
? `${basename(finalBinDir)}.zip`
32+
: `${basename(finalBinDir)}.tar.gz`;
33+
const finalCompressPath = resolve(binDir, finalCompressName);
34+
const checksumName = `${finalCompressName}.sha256`;
35+
const checksumPath = resolve(binDir, checksumName);
36+
const finalBinPath = resolve(finalBinDir, finalBinName);
37+
const compressCmd = target.includes("windows")
38+
? `zip -j ${finalCompressPath} ${finalBinPath}`
39+
: `tar -czvf ${finalCompressPath} -C ${finalBinDir} ${finalBinName}`;
40+
const scriptPath = resolve(rootDir, "src", "main.ts");
41+
42+
await $`deno compile --allow-env=ENV --allow-net=127.0.0.1 --cached-only --env=${
43+
$.path(resolve(rootDir, ".env"))
44+
} --target=${target} --output ${$.path(finalBinPath)} ${$.path(scriptPath)}`;
45+
await $.raw`${compressCmd}`;
46+
await $`cd ${$.path(binDir)} && sha256sum ${finalCompressName} > ${
47+
$.path(checksumPath)
48+
}`;
49+
await Deno.remove(finalBinDir, { recursive: true });
50+
};
51+
52+
await $`echo "ENV=production" > ${$.path(resolve(rootDir, ".env"))}`;
53+
54+
await Promise.all(targets.map(buildFor));

scripts/public-to-json.ts renamed to scripts/bundle.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { build, type Plugin, stop } from "npm:esbuild@0.23.1";
44
import { denoPlugins } from "jsr:@luca/esbuild-deno-loader@0.10.3";
55
import { resolve } from "@std/path";
66

7-
let htmlFile = Deno.readTextFileSync(
8-
new URL("../src/public/index.html", import.meta.url),
9-
);
7+
const rootDir = resolve(import.meta.dirname!, "../");
8+
const dataDir = resolve(rootDir, "data");
9+
const bundleFilePath = resolve(dataDir, "index.html");
1010

1111
const [jsCode, cssCode] = await Promise.all([
1212
build({
@@ -17,11 +17,11 @@ const [jsCode, cssCode] = await Promise.all([
1717
},
1818
},
1919
entryPoints: [
20-
resolve(import.meta.dirname!, "../src/public/src/main.tsx"),
20+
resolve(rootDir, "src/public/src/main.tsx"),
2121
],
2222
plugins: denoPlugins({
23-
configPath: resolve(import.meta.dirname!, "../deno.json"),
24-
lockPath: resolve(import.meta.dirname!, "../deno.lock"),
23+
configPath: resolve(rootDir, "deno.json"),
24+
lockPath: resolve(rootDir, "deno.lock"),
2525
}),
2626
define: {
2727
NODE_ENV: "production",
@@ -39,7 +39,7 @@ const [jsCode, cssCode] = await Promise.all([
3939
build({
4040
bundle: true,
4141
entryPoints: [
42-
resolve(import.meta.dirname!, "../src/public/css/main.css"),
42+
resolve(rootDir, "src/public/css/main.css"),
4343
],
4444
plugins: [
4545
{
@@ -69,6 +69,8 @@ const [jsCode, cssCode] = await Promise.all([
6969
}),
7070
]);
7171

72+
let htmlFile = Deno.readTextFileSync(resolve(rootDir, "src/public/index.html"));
73+
7274
htmlFile = htmlFile.replace(
7375
"{{ CssItems }}",
7476
cssCode.outputFiles.filter(({ path }) => path.endsWith(".css")).map((
@@ -86,16 +88,16 @@ htmlFile = htmlFile.replace(
8688
);
8789

8890
try {
89-
Deno.statSync("./src/public.json");
90-
Deno.removeSync("./src/public.json");
91+
Deno.mkdirSync(dataDir);
9192
// deno-lint-ignore no-empty
9293
} catch {}
9394

94-
Deno.writeTextFileSync(
95-
"./src/public.json",
96-
JSON.stringify({
97-
"index.html": Array.from(new TextEncoder().encode(htmlFile)),
98-
}),
99-
);
95+
try {
96+
Deno.statSync(bundleFilePath);
97+
Deno.removeSync(bundleFilePath);
98+
// deno-lint-ignore no-empty
99+
} catch {}
100+
101+
Deno.writeTextFileSync(bundleFilePath, htmlFile);
100102

101103
await stop();

scripts/embed.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env -S deno run -A --no-lock
2+
3+
import { resolve } from "jsr:@std/path@1.0.6";
4+
5+
const rootDir = resolve(import.meta.dirname!, "../");
6+
const dataDir = resolve(rootDir, "data");
7+
const embedFilePath = resolve(rootDir, "embed.json");
8+
9+
const embed: Record<string, unknown> = {};
10+
11+
for (const file of Deno.readDirSync(dataDir)) {
12+
if (file.name !== "index.html") continue;
13+
14+
embed[file.name] = Array.from(Deno.readFileSync(resolve(dataDir, file.name)));
15+
}
16+
17+
Deno.writeTextFileSync(embedFilePath, JSON.stringify(embed));

scripts/generate-builds.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const tag = new Command()
4545
throw new Error(`Permission to read/write to "${dir}" not granted.`);
4646
}
4747

48-
const embed = await import("./public.json", { with: { type: "json" } })
48+
const embed = await import("./../embed.json", { with: { type: "json" } })
4949
.then(({ default: main }) => main);
5050

5151
const ui = Uint8Array.from(embed["index.html"]);

0 commit comments

Comments
 (0)