import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { spawnSync } from "node:child_process"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const bun = process.env.BUN_BIN || "bun"; const args = process.argv.slice(2); if (args.some(arg => !arg.startsWith("--outfile="))) { throw new Error("Usage: build-plugin.mjs [--outfile=]"); } function run(args) { const result = spawnSync(bun, args, { cwd: root, stdio: "inherit", }); if (result.error) throw result.error; if (result.status !== 0) { throw new Error(`${bun} ${args.join(" ")} failed with status ${result.status}`); } } const mcpOutput = resolve(args.find(arg => arg.startsWith("--outfile="))?.slice("--outfile=".length) || resolve(root, "dist/mcp/server.js")); mkdirSync(dirname(mcpOutput), { recursive: true }); // The shared CLI uses bun:sqlite; the plugin manifest must use this runtime too. // Embedded CommonJS loaders must use the bundle path, not the build host's absolute checkout path. run(["build", "src/server.ts", "--minify", "--target=bun", "--define", "__filename=import.meta.path", "--outfile", mcpOutput]); writeFileSync(mcpOutput, readFileSync(mcpOutput, "utf8").replace(/[ \t]+$/gm, "")); console.log(`✓ Successfully bundled MCP server: ${mcpOutput}`);