-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
70 lines (61 loc) · 2.12 KB
/
Copy pathbuild.mjs
File metadata and controls
70 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
// Build script: bundles the CLI and the self-contained viewer HTML into dist/.
import * as esbuild from "esbuild";
import { copyFileSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const root = dirname(fileURLToPath(import.meta.url));
const watch = process.argv.includes("--watch");
/** @type {import('esbuild').BuildOptions} */
const shared = {
bundle: true,
minify: true,
logLevel: "info",
};
async function build() {
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
// 1. CLI
await esbuild.build({
...shared,
entryPoints: [join(root, "src/cli.ts")],
outfile: join(root, "dist/cli.js"),
platform: "node",
format: "esm",
target: "node18",
banner: { js: "#!/usr/bin/env node" },
define: { __AGENTOSCOPE_VERSION__: JSON.stringify(pkg.version) },
});
// 2. Viewer JS (browser app, marked bundled in)
const viewer = await esbuild.build({
...shared,
entryPoints: [join(root, "src/viewer/app.ts")],
platform: "browser",
format: "iife",
target: "es2020",
write: false,
});
const viewerJs = viewer.outputFiles[0].text;
const css = readFileSync(join(root, "src/viewer/styles.css"), "utf8");
const shell = readFileSync(join(root, "src/viewer/shell.html"), "utf8");
const html = shell
.replace("/*__AGENTOSCOPE_CSS__*/", () => css)
.replace("//__AGENTOSCOPE_JS__", () => viewerJs);
mkdirSync(join(root, "dist"), { recursive: true });
writeFileSync(join(root, "dist/viewer.html"), html);
console.log("dist/viewer.html written");
// 3. Hosted worker (agentoscope.rafael.ltd/r/:owner/:repo) + viewer template asset
await esbuild.build({
...shared,
entryPoints: [join(root, "src/worker.ts")],
outfile: join(root, "site/_worker.js"),
platform: "neutral",
format: "esm",
target: "es2022",
});
copyFileSync(join(root, "dist/viewer.html"), join(root, "site/_viewer.html"));
console.log("site/_worker.js + site/_viewer.html written");
}
build().catch((err) => {
console.error(err);
process.exit(1);
});