109 lines
4.3 KiB
TypeScript
109 lines
4.3 KiB
TypeScript
#!/usr/bin/env bun
|
|
/**
|
|
* Generate `pnpm-workspace.yaml` mirroring bun's `package.json#workspaces.*`
|
|
* fields so pnpm can resolve `catalog:` and `workspace:*` refs in this repo.
|
|
*
|
|
* `bun deploy:smoke` (and the GitHub Actions matrix `runtime: pnpm` job)
|
|
* call this script before `pnpm install` so pnpm has a coherent view of the
|
|
* workspace. The file is `.gitignore`d — never check it in.
|
|
*
|
|
* Usage:
|
|
* bun ./scripts/pnpm-workspace.ts # write
|
|
* bun ./scripts/pnpm-workspace.ts --remove # delete
|
|
*/
|
|
import * as fs from "node:fs/promises";
|
|
import * as path from "node:path";
|
|
|
|
const ROOT = path.resolve(import.meta.dir, "..");
|
|
const PNPM_WORKSPACE_PATH = path.join(ROOT, "pnpm-workspace.yaml");
|
|
|
|
if (process.argv.includes("--remove")) {
|
|
await fs.rm(PNPM_WORKSPACE_PATH, { force: true });
|
|
console.log(`removed ${path.relative(ROOT, PNPM_WORKSPACE_PATH)}`);
|
|
process.exit(0);
|
|
}
|
|
|
|
const pkg = JSON.parse(
|
|
await fs.readFile(path.join(ROOT, "package.json"), "utf8"),
|
|
) as {
|
|
workspaces?: {
|
|
packages?: string[];
|
|
catalog?: Record<string, string>;
|
|
};
|
|
};
|
|
const packages = pkg.workspaces?.packages ?? [];
|
|
const catalog = pkg.workspaces?.catalog ?? {};
|
|
|
|
// Bun's `catalog:` ranges are loose (`>=4.0.0-beta.60 || >=4.0.0`) and
|
|
// rely on `bun.lock` to pin the resolved version. pnpm without a lockfile
|
|
// re-resolves each `catalog:` ref to the freshest matching version, which
|
|
// lets sibling packages drift onto different `[email protected]{2,4}`
|
|
// builds and produces "Effect<...beta.62> not assignable to Effect<...beta.64>"
|
|
// type errors in CI. Mirror bun's resolved versions into the pnpm catalog
|
|
// so both runtimes see identical trees.
|
|
const bunLockText = await fs
|
|
.readFile(path.join(ROOT, "bun.lock"), "utf8")
|
|
.catch(() => "");
|
|
const resolveFromBunLock = (name: string): string | undefined => {
|
|
// Each top-level package entry in bun.lock looks like:
|
|
// "<name>": ["<name>@<version>", ...]
|
|
// (bun.lock is JSONC with trailing commas, so we regex it.)
|
|
const escaped = name.replace(/[/@.+*?(){}[\]^$|\\-]/g, "\\$&");
|
|
const re = new RegExp(`"${escaped}":\\s*\\["${escaped}@([^"]+)"`);
|
|
return bunLockText.match(re)?.[1];
|
|
};
|
|
const pinnedCatalog: Record<string, string> = {};
|
|
for (const [name, range] of Object.entries(catalog)) {
|
|
// Non-semver catalog values (URLs, file: paths, workspace:* entries from
|
|
// bun.lock for monorepo packages) must pass through unchanged — pnpm
|
|
// rejects e.g. `alchemy: workspace:packages/alchemy` in a catalog
|
|
// (ERR_PNPM_CATALOG_ENTRY_INVALID_WORKSPACE_SPEC).
|
|
if (/^(https?:|file:|git\+|workspace:|npm:)/.test(range)) {
|
|
pinnedCatalog[name] = range;
|
|
continue;
|
|
}
|
|
const pinned = resolveFromBunLock(name);
|
|
pinnedCatalog[name] =
|
|
pinned && !/^(workspace:|file:|link:)/.test(pinned) ? pinned : range;
|
|
}
|
|
|
|
const lines: string[] = [
|
|
"# Auto-generated by scripts/pnpm-workspace.ts — DO NOT COMMIT.",
|
|
"# Mirrors `package.json#workspaces.{packages,catalog}` so pnpm can",
|
|
"# resolve `catalog:` deps in our examples (bun keeps catalogs in",
|
|
"# `package.json#workspaces.catalog`; pnpm only reads them from this",
|
|
"# file).",
|
|
"",
|
|
// pnpm 11 fails the install with `ERR_PNPM_IGNORED_BUILDS` whenever
|
|
// a transitive dep has a postinstall script that hasn't been
|
|
// explicitly approved. Whitelist only the ones our examples actually
|
|
// need to build (workerd binary download, esbuild's optional native
|
|
// binaries, sharp's libvips, etc.).
|
|
//
|
|
// pnpm 11 renamed `onlyBuiltDependencies` (list) → `allowBuilds`
|
|
// (dict). Emit both so the file works on pnpm 10 and pnpm 11; pnpm
|
|
// silently ignores unknown keys.
|
|
"onlyBuiltDependencies:",
|
|
...["@parcel/watcher", "esbuild", "msgpackr-extract", "sharp", "workerd"].map(
|
|
(name) => ` - ${JSON.stringify(name)}`,
|
|
),
|
|
"",
|
|
"allowBuilds:",
|
|
...["@parcel/watcher", "esbuild", "msgpackr-extract", "sharp", "workerd"].map(
|
|
(name) => ` ${JSON.stringify(name)}: true`,
|
|
),
|
|
"",
|
|
"packages:",
|
|
...packages.map((p) => ` - ${JSON.stringify(p)}`),
|
|
];
|
|
if (Object.keys(pinnedCatalog).length > 0) {
|
|
lines.push("", "catalog:");
|
|
for (const [name, range] of Object.entries(pinnedCatalog)) {
|
|
lines.push(` ${JSON.stringify(name)}: ${JSON.stringify(range)}`);
|
|
}
|
|
}
|
|
lines.push("");
|
|
|
|
await fs.writeFile(PNPM_WORKSPACE_PATH, lines.join("\n"));
|
|
console.log(`wrote ${path.relative(ROOT, PNPM_WORKSPACE_PATH)}`);
|