|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { chmodSync, copyFileSync, mkdirSync, readdirSync, rmSync } from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | + |
| 6 | +const repoRoot = path.resolve(import.meta.dir, ".."); |
| 7 | +const outdir = path.join(repoRoot, "dist", "npm"); |
| 8 | +const typesOutdir = path.join(repoRoot, "dist", "npm-types"); |
| 9 | +const opentuiOutdir = path.join(outdir, "opentui"); |
| 10 | +const opentuiTypesDir = path.join(typesOutdir, "opentui"); |
| 11 | + |
| 12 | +const bunEnv = { |
| 13 | + ...process.env, |
| 14 | + BUN_TMPDIR: path.join(repoRoot, ".bun-tmp"), |
| 15 | + BUN_INSTALL: path.join(repoRoot, ".bun-install"), |
| 16 | +}; |
| 17 | + |
| 18 | +function runBun(args: string[]) { |
| 19 | + const proc = Bun.spawnSync(["bun", ...args], { |
| 20 | + cwd: repoRoot, |
| 21 | + stdin: "inherit", |
| 22 | + stdout: "inherit", |
| 23 | + stderr: "inherit", |
| 24 | + env: bunEnv, |
| 25 | + }); |
| 26 | + |
| 27 | + if (proc.exitCode !== 0) { |
| 28 | + throw new Error(`bun ${args.join(" ")} failed with exit ${proc.exitCode}`); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +rmSync(outdir, { recursive: true, force: true }); |
| 33 | +rmSync(typesOutdir, { recursive: true, force: true }); |
| 34 | +mkdirSync(opentuiOutdir, { recursive: true }); |
| 35 | + |
| 36 | +runBun([ |
| 37 | + "build", |
| 38 | + path.join(repoRoot, "src", "main.tsx"), |
| 39 | + "--target", |
| 40 | + "bun", |
| 41 | + "--format", |
| 42 | + "esm", |
| 43 | + "--outdir", |
| 44 | + outdir, |
| 45 | + "--entry-naming", |
| 46 | + "main.js", |
| 47 | +]); |
| 48 | + |
| 49 | +const mainJs = path.join(outdir, "main.js"); |
| 50 | +// chmod is a no-op on Windows; preserve exec bits on Unix so the bin runs in npm-installed packages. |
| 51 | +if (process.platform !== "win32") { |
| 52 | + chmodSync(mainJs, 0o755); |
| 53 | +} |
| 54 | + |
| 55 | +runBun([ |
| 56 | + "build", |
| 57 | + path.join(repoRoot, "src", "opentui", "index.ts"), |
| 58 | + "--target", |
| 59 | + "node", |
| 60 | + "--format", |
| 61 | + "esm", |
| 62 | + "--external", |
| 63 | + "react", |
| 64 | + "--external", |
| 65 | + "react/jsx-runtime", |
| 66 | + "--external", |
| 67 | + "react/jsx-dev-runtime", |
| 68 | + "--external", |
| 69 | + "@opentui/core", |
| 70 | + "--external", |
| 71 | + "@opentui/react", |
| 72 | + "--external", |
| 73 | + "@opentui/react/jsx-runtime", |
| 74 | + "--external", |
| 75 | + "@opentui/react/jsx-dev-runtime", |
| 76 | + "--external", |
| 77 | + "@pierre/diffs", |
| 78 | + "--outdir", |
| 79 | + opentuiOutdir, |
| 80 | + "--entry-naming", |
| 81 | + "index.js", |
| 82 | +]); |
| 83 | + |
| 84 | +runBun(["x", "tsc", "-p", path.join(repoRoot, "tsconfig.opentui.json")]); |
| 85 | + |
| 86 | +for (const entry of readdirSync(opentuiTypesDir)) { |
| 87 | + if (entry.endsWith(".d.ts")) { |
| 88 | + copyFileSync(path.join(opentuiTypesDir, entry), path.join(opentuiOutdir, entry)); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +rmSync(typesOutdir, { recursive: true, force: true }); |
| 93 | + |
| 94 | +console.log(`Built ${mainJs}`); |
| 95 | +console.log(`Built ${path.join(opentuiOutdir, "index.js")}`); |
0 commit comments