|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { $ } from "bun"; |
| 4 | +import { join, resolve } from "node:path"; |
| 5 | +import { runWithExitCode } from "../src/runnable"; |
| 6 | + |
| 7 | +const REPO_ROOT = resolve(import.meta.dir, ".."); |
| 8 | +const ASSETS_DIR = join(REPO_ROOT, "src", "assets"); |
| 9 | +const ENTRYPOINT = join(REPO_ROOT, "src", "index.ts"); |
| 10 | +const DIST = join(REPO_ROOT, "dist"); |
| 11 | + |
| 12 | +const ASSET_NAMING = "agentcore-assets/[dir]/[name].[ext]"; |
| 13 | + |
| 14 | +// Shrink whitespace/syntax but keep identifiers: minified names make stack |
| 15 | +// traces unreadable and erase error names telemetry keys on. |
| 16 | +const MINIFY = { whitespace: true, syntax: true, identifiers: false } as const; |
| 17 | + |
| 18 | +/** Absolute paths of every asset file. dot:true so hidden files (.prettierrc) are included. */ |
| 19 | +function discoverAssets(): string[] { |
| 20 | + const files = [...new Bun.Glob("**/*").scanSync({ cwd: ASSETS_DIR, onlyFiles: true, dot: true })]; |
| 21 | + return files.sort().map((relativePath) => join(ASSETS_DIR, relativePath)); |
| 22 | +} |
| 23 | + |
| 24 | +/** Force asset files through the file loader so template .ts/.js are embedded as bytes, not compiled. */ |
| 25 | +function assetLoaderPlugin(): Bun.BunPlugin { |
| 26 | + return { |
| 27 | + name: "asset-file-loader", |
| 28 | + setup(build) { |
| 29 | + build.onLoad({ filter: /src[/\\]assets[/\\]/ }, async ({ path }) => ({ |
| 30 | + contents: await Bun.file(path).bytes(), |
| 31 | + loader: "file", |
| 32 | + })); |
| 33 | + }, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** Fail loudly on a non-UTF-8 asset — the source reads every asset as text. */ |
| 38 | +async function assertAssetsAreText(assets: string[]): Promise<void> { |
| 39 | + const decoder = new TextDecoder("utf-8", { fatal: true }); |
| 40 | + for (const path of assets) { |
| 41 | + try { |
| 42 | + decoder.decode(await Bun.file(path).bytes()); |
| 43 | + } catch { |
| 44 | + throw new Error(`Asset is not valid UTF-8: ${path}`); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Bun.build rejects with an AggregateError on failure (throw defaults to true), |
| 50 | +// so build errors propagate to runWithExitCode like any other. |
| 51 | +async function bundle(): Promise<void> { |
| 52 | + await Bun.build({ |
| 53 | + entrypoints: [ENTRYPOINT], |
| 54 | + outdir: DIST, |
| 55 | + target: "node", |
| 56 | + minify: MINIFY, |
| 57 | + }); |
| 58 | + |
| 59 | + // Mirror assets beside the emitted module for resolveAssetsRoot(). |
| 60 | + const distAssets = join(DIST, "assets"); |
| 61 | + await $`rm -rf ${distAssets}`; |
| 62 | + await $`cp -R ${ASSETS_DIR} ${distAssets}`; |
| 63 | + console.log(`Bundled to ${join(DIST, "index.js")} with assets/`); |
| 64 | +} |
| 65 | + |
| 66 | +async function compile(target: string): Promise<void> { |
| 67 | + const assets = discoverAssets(); |
| 68 | + await assertAssetsAreText(assets); |
| 69 | + |
| 70 | + const outfile = join(DIST, "bin", `agentcore-${target.replace(/^bun-/, "")}`); |
| 71 | + await $`mkdir -p ${join(DIST, "bin")}`; |
| 72 | + |
| 73 | + await Bun.build({ |
| 74 | + entrypoints: [ENTRYPOINT, ...assets], |
| 75 | + compile: { target: target as Bun.Build.CompileTarget, outfile }, |
| 76 | + minify: MINIFY, |
| 77 | + root: REPO_ROOT, |
| 78 | + naming: { asset: ASSET_NAMING }, |
| 79 | + plugins: [assetLoaderPlugin()], |
| 80 | + }); |
| 81 | + console.log(`Compiled ${target} → ${outfile} (${assets.length} assets embedded)`); |
| 82 | +} |
| 83 | + |
| 84 | +process.exit( |
| 85 | + await runWithExitCode(async () => { |
| 86 | + const [command, target] = process.argv.slice(2); |
| 87 | + |
| 88 | + if (command === "bundle") { |
| 89 | + await bundle(); |
| 90 | + } else if (command === "compile") { |
| 91 | + if (!target) { |
| 92 | + throw new Error("Usage: bun scripts/build.ts compile <bun-target>"); |
| 93 | + } |
| 94 | + await compile(target); |
| 95 | + } else { |
| 96 | + throw new Error("Usage: bun scripts/build.ts <bundle|compile <target>>"); |
| 97 | + } |
| 98 | + }), |
| 99 | +); |
0 commit comments