#!/usr/bin/env node import { execFileSync } from "node:child_process"; import { readFileSync, writeFileSync } from "node:fs"; import { resolve } from "node:path"; import { fileURLToPath } from "node:url"; const repoRoot = resolve(fileURLToPath(new URL("..", import.meta.url))); export function materializePublishManifest(pkg) { const publishConfig = pkg.publishConfig ?? {}; const publishManifest = { ...pkg }; for (const key of ["main", "types", "exports", "bin"]) { if (publishConfig[key] !== undefined) publishManifest[key] = publishConfig[key]; } delete publishManifest.publishConfig; return publishManifest; } export function prepareBundledPackage(sourceDir, destinationDir) { const sourcePackagePath = resolve(sourceDir, "package.json"); const sourcePackage = JSON.parse(readFileSync(sourcePackagePath, "utf8")); const bundledDependencies = sourcePackage.bundleDependencies ?? sourcePackage.bundledDependencies ?? []; if (bundledDependencies.length === 0) { throw new Error(`${sourcePackage.name} does not declare bundled dependencies`); } execFileSync( "pnpm", ["--filter", sourcePackage.name, "deploy", "--prod", resolve(destinationDir)], { cwd: repoRoot, stdio: "inherit" }, ); const deployedPackagePath = resolve(destinationDir, "package.json"); const deployedPackage = JSON.parse(readFileSync(deployedPackagePath, "utf8")); writeFileSync( deployedPackagePath, `${JSON.stringify(materializePublishManifest(deployedPackage), null, 2)}\n`, ); } if (process.argv[1] === fileURLToPath(import.meta.url)) { const [sourceDir, destinationDir] = process.argv.slice(2); if (!sourceDir || !destinationDir) { console.error("Usage: prepare-bundled-package.mjs "); process.exit(1); } prepareBundledPackage(resolve(sourceDir), resolve(destinationDir)); }