| 1 | import { $ } from "bun"; |
| 2 | import { rm } from "node:fs/promises"; |
| 3 | import { join, resolve } from "node:path"; |
| 4 | import packageJson from "../package.json" with { type: "json" }; |
| 5 | |
| 6 | const packageDirectory = resolve(import.meta.dir, ".."); |
| 7 | const now = performance.now(); |
| 8 | |
| 9 | const exports = Object.fromEntries( |
| 10 | Object.entries(packageJson.exports).map(([subpath, source]) => { |
| 11 | if (source === null) { |
| 12 | return [subpath, null]; |
| 13 | } |
| 14 | |
| 15 | // Bin entrypoints are plain JavaScript in both checkout and published use. |
| 16 | if (subpath.startsWith("./bin/")) { |
| 17 | return [subpath, source]; |
| 18 | } |
| 19 | |
| 20 | // Bootstrap modules are source inputs to the deployment bundler. |
| 21 | if (subpath === "./Runtime/Bootstrap/*") { |
| 22 | return [subpath, { types: source, bun: source, default: source }]; |
| 23 | } |
| 24 | |
| 25 | const output = source.replace(/^\.\/src\//, "./lib/"); |
| 26 | return [ |
| 27 | subpath, |
| 28 | { |
| 29 | types: output.replace(/\.tsx?$/, ".d.ts"), |
| 30 | bun: source, |
| 31 | default: output.replace(/\.tsx?$/, ".js"), |
| 32 | }, |
| 33 | ]; |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | await Bun.write( |
| 38 | join(packageDirectory, "package.json"), |
| 39 | JSON.stringify( |
| 40 | { |
| 41 | ...packageJson, |
| 42 | publishConfig: { ...packageJson.publishConfig, exports }, |
| 43 | }, |
| 44 | null, |
| 45 | 2, |
| 46 | ) + "\n", |
| 47 | ); |
| 48 | |
| 49 | await rm(join(packageDirectory, "lib"), { recursive: true, force: true }); |
| 50 | console.log(`Building ${packageJson.name} v${packageJson.version}...`); |
| 51 | await $`tsc -b --force`.cwd(packageDirectory); |
| 52 | await $`bun ../../scripts/copy-package-files.ts alchemy`.cwd(packageDirectory); |
| 53 | console.log(`Built in ${Math.round(performance.now() - now) / 1000}s`); |