Project Files
build.mjs
#!/usr/bin/env node
import { execSync } from "node:child_process";
import { rmSync, writeFileSync, existsSync, mkdirSync, statSync, chmodSync } from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
function clean() {
rmSync(resolve(__dirname, "dist"), { recursive: true, force: true });
rmSync(resolve(__dirname, "dist-temp"), { recursive: true, force: true });
}
function makeExec(p) {
try {
const st = statSync(p);
if ((st.mode & 0o111) === 0) chmodSync(p, 0o755);
} catch {}
}
try {
console.log("clean: removing dist...");
clean();
console.log("[rollup] step 1: compile TypeScript with SWC to dist-temp/src ...");
execSync("npx swc src -d dist-temp/src --config-file .swcrc", {
stdio: "inherit",
cwd: resolve(__dirname),
});
// Ensure entry file exists
const idxA = resolve(__dirname, "dist-temp", "src", "index.js");
const idxB = resolve(__dirname, "dist-temp", "src", "src", "index.js");
if (!existsSync(idxA) && existsSync(idxB)) {
mkdirSync(resolve(__dirname, "dist-temp", "src"), { recursive: true });
const shim = `#!/usr/bin/env node\nexport * from "./src/index.js";\nimport "./src/index.js";\n`;
writeFileSync(idxA, shim, "utf8");
makeExec(idxA);
console.log("[rollup] created entry shim dist-temp/src/index.js");
}
console.log("[rollup] step 2: bundle with Rollup ...");
execSync("npx rollup -c", { stdio: "inherit", cwd: resolve(__dirname) });
console.log("[rollup] step 3: cleanup dist-temp ...");
rmSync(resolve(__dirname, "dist-temp"), { recursive: true, force: true });
const cliPath = resolve(__dirname, "dist", "cli.js");
writeFileSync(cliPath, `#!/usr/bin/env node\nrequire('./index.js');\n`, "utf8");
makeExec(cliPath);
console.log("build complete.");
} catch (e) {
console.error("error during build:", e);
process.exit(e.status || 1);
}