Skip to content

Commit

Permalink
Improve build
Browse files Browse the repository at this point in the history
Signed-off-by: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com>
  • Loading branch information
M4RC3L05 committed Oct 1, 2024
1 parent 75924e2 commit 21fb7df
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: deno task deps

- name: build
run: ./scripts/generate-builds.ts
run: ./scripts/bundle.ts && ./scripts/embed.ts && ./scripts/build.ts

- name: release
uses: ncipollo/release-action@v1
Expand Down
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
.bin/*
!.bin/git-hooks
!.bin/.gitkeep
!.bin/public-to-json.ts
share
lib
include
data
src/public.json
*.dll
.cache
.env
embed.json
12 changes: 4 additions & 8 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
"version": "4.5.3",
"exports": "./src/main.ts",
"tasks": {
"run": "./scripts/public-to-json.ts && deno run --allow-env=ENV --allow-net=127.0.0.1 --cached-only src/main.ts",
"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",
"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",
"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",
"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",
"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",
"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",
"run": "./scripts/bundle.ts && ./scripts/embed.ts && deno run --allow-env=ENV --allow-net=127.0.0.1 --cached-only src/main.ts",
"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",
"deps:lock": "deno cache --frozen=false src/**/*.ts src/**/*.tsx",
"deps": "deno cache --reload src/**/*.ts src/**/*.tsx"
},
Expand All @@ -18,12 +13,13 @@
"jsx": "react-jsx"
},
"lint": {
"exclude": ["embed.json"],
"rules": {
"include": ["verbatim-module-syntax"]
}
},
"fmt": {
"exclude": ["./src/public.json"]
"exclude": ["embed.json"]
},
"imports": {
"@mjackson/multipart-parser": "jsr:@mjackson/multipart-parser@0.6.2",
Expand Down
54 changes: 54 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env -S deno run -A --no-lock

import $ from "jsr:@david/dax@0.42.0";
import { basename, resolve } from "@std/path";

const rootDir = resolve(import.meta.dirname!, "../");
const binDir = resolve(rootDir, ".bin");
const binName = "denotag" as const;

const targets = [
"x86_64-pc-windows-msvc",
"x86_64-apple-darwin",
"aarch64-apple-darwin",
"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
] as const;

for (const file of Deno.readDirSync("./.bin")) {
if (
file.name.match(/.*\.(zip|tar\.gz)(\.sha256)?$/) ||
targets.some((target) => file.name.includes(target))
) {
Deno.removeSync(resolve(binDir, file.name), { recursive: true });
}
}

const buildFor = async (target: typeof targets[number]) => {
const finalBinDir = resolve(binDir, `${binName}-${target}`);
const finalBinName = target.includes("windows") ? `${binName}.exe` : binName;
const finalCompressName = target.includes("windows")
? `${basename(finalBinDir)}.zip`
: `${basename(finalBinDir)}.tar.gz`;
const finalCompressPath = resolve(binDir, finalCompressName);
const checksumName = `${finalCompressName}.sha256`;
const checksumPath = resolve(binDir, checksumName);
const finalBinPath = resolve(finalBinDir, finalBinName);
const compressCmd = target.includes("windows")
? `zip -j ${finalCompressPath} ${finalBinPath}`
: `tar -czvf ${finalCompressPath} -C ${finalBinDir} ${finalBinName}`;
const scriptPath = resolve(rootDir, "src", "main.ts");

await $`deno compile --allow-env=ENV --allow-net=127.0.0.1 --cached-only --env=${
$.path(resolve(rootDir, ".env"))
} --target=${target} --output ${$.path(finalBinPath)} ${$.path(scriptPath)}`;
await $.raw`${compressCmd}`;
await $`cd ${$.path(binDir)} && sha256sum ${finalCompressName} > ${
$.path(checksumPath)
}`;
await Deno.remove(finalBinDir, { recursive: true });
};

await $`echo "ENV=production" > ${$.path(resolve(rootDir, ".env"))}`;

await Promise.all(targets.map(buildFor));
32 changes: 17 additions & 15 deletions scripts/public-to-json.ts → scripts/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { build, type Plugin, stop } from "npm:esbuild@0.23.1";
import { denoPlugins } from "jsr:@luca/esbuild-deno-loader@0.10.3";
import { resolve } from "@std/path";

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

const [jsCode, cssCode] = await Promise.all([
build({
Expand All @@ -17,11 +17,11 @@ const [jsCode, cssCode] = await Promise.all([
},
},
entryPoints: [
resolve(import.meta.dirname!, "../src/public/src/main.tsx"),
resolve(rootDir, "src/public/src/main.tsx"),
],
plugins: denoPlugins({
configPath: resolve(import.meta.dirname!, "../deno.json"),
lockPath: resolve(import.meta.dirname!, "../deno.lock"),
configPath: resolve(rootDir, "deno.json"),
lockPath: resolve(rootDir, "deno.lock"),
}),
define: {
NODE_ENV: "production",
Expand All @@ -39,7 +39,7 @@ const [jsCode, cssCode] = await Promise.all([
build({
bundle: true,
entryPoints: [
resolve(import.meta.dirname!, "../src/public/css/main.css"),
resolve(rootDir, "src/public/css/main.css"),
],
plugins: [
{
Expand Down Expand Up @@ -69,6 +69,8 @@ const [jsCode, cssCode] = await Promise.all([
}),
]);

let htmlFile = Deno.readTextFileSync(resolve(rootDir, "src/public/index.html"));

htmlFile = htmlFile.replace(
"{{ CssItems }}",
cssCode.outputFiles.filter(({ path }) => path.endsWith(".css")).map((
Expand All @@ -86,16 +88,16 @@ htmlFile = htmlFile.replace(
);

try {
Deno.statSync("./src/public.json");
Deno.removeSync("./src/public.json");
Deno.mkdirSync(dataDir);
// deno-lint-ignore no-empty
} catch {}

Deno.writeTextFileSync(
"./src/public.json",
JSON.stringify({
"index.html": Array.from(new TextEncoder().encode(htmlFile)),
}),
);
try {
Deno.statSync(bundleFilePath);
Deno.removeSync(bundleFilePath);
// deno-lint-ignore no-empty
} catch {}

Deno.writeTextFileSync(bundleFilePath, htmlFile);

await stop();
17 changes: 17 additions & 0 deletions scripts/embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env -S deno run -A --no-lock

import { resolve } from "jsr:@std/path@1.0.6";

const rootDir = resolve(import.meta.dirname!, "../");
const dataDir = resolve(rootDir, "data");
const embedFilePath = resolve(rootDir, "embed.json");

const embed: Record<string, unknown> = {};

for (const file of Deno.readDirSync(dataDir)) {
if (file.name !== "index.html") continue;

embed[file.name] = Array.from(Deno.readFileSync(resolve(dataDir, file.name)));
}

Deno.writeTextFileSync(embedFilePath, JSON.stringify(embed));
34 changes: 0 additions & 34 deletions scripts/generate-builds.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const tag = new Command()
throw new Error(`Permission to read/write to "${dir}" not granted.`);
}

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

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

0 comments on commit 21fb7df

Please sign in to comment.