#!/usr/bin/env bun import fs from "fs" import path from "path" import { fileURLToPath } from "url" const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) const cliDir = path.resolve(__dirname, "..") const repoRoot = path.resolve(cliDir, "../..") const distDir = path.resolve(repoRoot, "frontend/workspace/dist") const outFile = path.resolve(cliDir, "src/web/assets.generated.ts") if (!fs.existsSync(distDir)) { throw new Error( `frontend/workspace/dist not found at ${distDir} — run \`cd frontend/workspace && bun run build\` first`, ) } function walk(dir: string, out: string[] = []): string[] { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name) if (entry.isDirectory()) walk(full, out) else if (entry.isFile()) out.push(full) } return out } const files = walk(distDir).sort() const outDir = path.dirname(outFile) fs.mkdirSync(outDir, { recursive: true }) const lines: string[] = [ "// AUTO-GENERATED by script/generate-web-assets.ts — do not edit by hand.", "// Regenerated by `backend/cli/script/build.ts` after every vite build of frontend/workspace.", "// @ts-nocheck", "", ] const entries: string[] = [] files.forEach((absPath, i) => { const rel = path.relative(distDir, absPath).replaceAll("\\", "/") const ident = `_${i}` const importRel = path.relative(outDir, absPath).replaceAll("\\", "/") const importSpec = importRel.startsWith(".") ? importRel : `./${importRel}` lines.push(`import ${ident} from "${importSpec}" with { type: "file" }`) entries.push(` "/${rel}": ${ident},`) }) lines.push("") lines.push('// Each value is the runtime path injected by Bun\'s `with { type: "file" }` loader.') lines.push("// Typed loosely because tsgo doesn't model that loader's return type uniformly.") lines.push("export const WEB_ASSETS: Record = {") for (const e of entries) lines.push(e.replace(",", " as unknown as string,")) lines.push("}") lines.push("") lines.push('export const WEB_INDEX: string | undefined = WEB_ASSETS["/index.html"]') lines.push("") fs.writeFileSync(outFile, lines.join("\n")) console.log(`generate-web-assets: wrote ${files.length} entries → ${path.relative(repoRoot, outFile)}`)