refactor: replace bash build script with TypeScript

This commit is contained in:
2026-06-21 01:11:38 +02:00
committed by Milas Holsting
parent 30a23dae5e
commit c0808fe5f3
2 changed files with 70 additions and 26 deletions

View File

@@ -1,26 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
started_at=$(date +%s%N)
steps=(
"player:bun build ./static/player/main.ts --outdir ./dist/static/player --target browser --splitting"
"app:bun build ./static/app.ts --outdir ./dist/static --target browser --root ./static --entry-naming [name].js"
)
for step in "${steps[@]}"; do
name="${step%%:*}"
cmd="${step#*:}"
echo "building $name..."
if ! eval "$cmd"; then
echo "ts build failed at $name" >&2
exit 1
fi
done
mkdir -p ./dist/static
cp ./node_modules/htmx.org/dist/htmx.min.js ./dist/static/htmx-lib.js
total_entries=2
elapsed_ms=$((($(date +%s%N) - started_at) / 1000000))
echo "ts build ok ($total_entries entries, ${elapsed_ms}ms)"

70
scripts/build-ts.ts Normal file
View File

@@ -0,0 +1,70 @@
import { spawnSync } from "node:child_process";
import { cp, mkdir } from "node:fs/promises";
import { performance } from "node:perf_hooks";
type BuildStep = {
name: string;
args: string[];
};
const buildSteps: BuildStep[] = [
{
name: "player",
args: [
"build",
"./static/player/main.ts",
"--outdir",
"./dist/static/player",
"--target",
"browser",
"--splitting",
],
},
{
name: "app",
args: [
"build",
"./static/app.ts",
"--outdir",
"./dist/static",
"--target",
"browser",
"--root",
"./static",
"--entry-naming",
"[name].js",
],
},
];
const runBuildStep = (step: BuildStep): void => {
console.log(`building ${step.name}...`);
const result = spawnSync("bun", step.args, { stdio: "inherit" });
if (result.error) {
throw new Error(`failed to start ${step.name} build: ${result.error.message}`);
}
if (result.signal) {
throw new Error(`ts build interrupted at ${step.name} (${result.signal})`);
}
if (result.status !== 0) {
throw new Error(`ts build failed at ${step.name}`);
}
};
const main = async (): Promise<void> => {
const startedAt = performance.now();
buildSteps.forEach(runBuildStep);
await mkdir("./dist/static", { recursive: true });
await cp("./node_modules/htmx.org/dist/htmx.min.js", "./dist/static/htmx-lib.js");
const elapsedMs = Math.round(performance.now() - startedAt);
console.log(`ts build ok (${buildSteps.length} entries, ${elapsedMs}ms)`);
};
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
});