1import { $ } from "bun";
2import { rm } from "node:fs/promises";
3import { join, resolve } from "node:path";
4import packageJson from "../package.json" with { type: "json" };
5
6const packageDirectory = resolve(import.meta.dir, "..");
7const now = performance.now();
8
9const 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
37await 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
49await rm(join(packageDirectory, "lib"), { recursive: true, force: true });
50console.log(`Building ${packageJson.name} v${packageJson.version}...`);
51await $`tsc -b --force`.cwd(packageDirectory);
52await $`bun ../../scripts/copy-package-files.ts alchemy`.cwd(packageDirectory);
53console.log(`Built in ${Math.round(performance.now() - now) / 1000}s`);