From 5980af43cae257325c22a6d1ca269c64abc45cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 26 Mar 2026 14:46:54 -0400 Subject: [PATCH] fix: move workspace manifest rewrite to prepack --- .github/workflows/publish.yml | 2 +- package.json | 2 +- packages/cli/package.json | 4 +- packages/engine/package.json | 4 +- packages/producer/package.json | 2 + packages/studio/package.json | 4 +- .../prepare-package-manifest-for-publish.mjs | 87 +++++++++++++++++++ scripts/prepare-publish-manifests.ts | 87 ------------------- .../restore-package-manifest-after-pack.mjs | 32 +++++++ scripts/verify-packed-manifests.mjs | 73 ++++++++++++++++ 10 files changed, 205 insertions(+), 92 deletions(-) create mode 100644 scripts/prepare-package-manifest-for-publish.mjs delete mode 100644 scripts/prepare-publish-manifests.ts create mode 100644 scripts/restore-package-manifest-after-pack.mjs create mode 100644 scripts/verify-packed-manifests.mjs diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 1f54b29497..57ac5a941d 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -23,7 +23,7 @@ jobs: registry-url: "https://registry.npmjs.org" - run: bun install --frozen-lockfile - run: bun run build - - run: bun run prepare:publish-manifests + - run: bun run verify:packed-manifests - name: Publish @hyperframes/core continue-on-error: true diff --git a/package.json b/package.json index 185cb5609b..507f5d0391 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "studio": "bun run --filter @hyperframes/studio dev", "build:hyperframes-runtime": "bun run --filter @hyperframes/core build:hyperframes-runtime", "build:hyperframes-runtime:modular": "bun run --filter @hyperframes/core build:hyperframes-runtime:modular", - "prepare:publish-manifests": "tsx scripts/prepare-publish-manifests.ts", + "verify:packed-manifests": "node scripts/verify-packed-manifests.mjs", "set-version": "tsx scripts/set-version.ts", "lint": "oxlint .", "lint:fix": "oxlint --fix .", diff --git a/packages/cli/package.json b/packages/cli/package.json index 2917f13a2b..56e1819361 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -15,7 +15,9 @@ "build:studio": "cd ../studio && bun run build", "build:runtime": "tsx scripts/build-runtime.ts", "build:copy": "mkdir -p dist/studio dist/docs dist/templates && cp -r ../studio/dist/* dist/studio/ && cp -r src/templates/blank src/templates/warm-grain src/templates/play-mode src/templates/swiss-grid src/templates/vignelli dist/templates/ && (cp src/docs/*.md dist/docs/ 2>/dev/null || true)", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "prepack": "node ../../scripts/prepare-package-manifest-for-publish.mjs", + "postpack": "node ../../scripts/restore-package-manifest-after-pack.mjs" }, "dependencies": { "@hono/node-server": "^1.8.0", diff --git a/packages/engine/package.json b/packages/engine/package.json index bc12f60549..112383fc8c 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -11,7 +11,9 @@ "scripts": { "build": "tsc", "test": "vitest run", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "prepack": "node ../../scripts/prepare-package-manifest-for-publish.mjs", + "postpack": "node ../../scripts/restore-package-manifest-after-pack.mjs" }, "dependencies": { "@hono/node-server": "^1.13.0", diff --git a/packages/producer/package.json b/packages/producer/package.json index 4d58612631..0326d3ad0a 100644 --- a/packages/producer/package.json +++ b/packages/producer/package.json @@ -36,6 +36,8 @@ "docker:build:test": "docker build -f ../../Dockerfile.test -t hyperframes-producer:test ../..", "docker:test": "docker run --rm --security-opt seccomp=unconfined --shm-size=2g -v ./tests:/app/packages/producer/tests hyperframes-producer:test", "docker:test:update": "docker run --rm --security-opt seccomp=unconfined --shm-size=2g -v ./tests:/app/packages/producer/tests hyperframes-producer:test --update", + "prepack": "node ../../scripts/prepare-package-manifest-for-publish.mjs", + "postpack": "node ../../scripts/restore-package-manifest-after-pack.mjs", "prepublishOnly": "echo skip" }, "dependencies": { diff --git a/packages/studio/package.json b/packages/studio/package.json index 74a21a230a..06425c2256 100644 --- a/packages/studio/package.json +++ b/packages/studio/package.json @@ -15,7 +15,9 @@ "scripts": { "dev": "vite", "build": "vite build", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "prepack": "node ../../scripts/prepare-package-manifest-for-publish.mjs", + "postpack": "node ../../scripts/restore-package-manifest-after-pack.mjs" }, "dependencies": { "@codemirror/autocomplete": "^6.20.1", diff --git a/scripts/prepare-package-manifest-for-publish.mjs b/scripts/prepare-package-manifest-for-publish.mjs new file mode 100644 index 0000000000..87bdccf6c5 --- /dev/null +++ b/scripts/prepare-package-manifest-for-publish.mjs @@ -0,0 +1,87 @@ +#!/usr/bin/env node + +import { createHash } from "node:crypto"; +import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { tmpdir } from "node:os"; + +const ROOT = join(import.meta.dirname, ".."); +const PACKAGES_DIR = join(ROOT, "packages"); +const BACKUP_DIR = join(tmpdir(), "hyperframes-publish-manifest-backups"); +const DEP_FIELDS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"]; + +function loadPackageJson(path) { + return JSON.parse(readFileSync(path, "utf8")); +} + +function listWorkspacePackages() { + return readdirSync(PACKAGES_DIR) + .map((dir) => join(PACKAGES_DIR, dir, "package.json")) + .filter((path) => existsSync(path)); +} + +function getBackupPath(packageJsonPath) { + const digest = createHash("sha256").update(packageJsonPath).digest("hex"); + return join(BACKUP_DIR, `${digest}.json`); +} + +function resolveWorkspaceSpec(spec, version, packageJsonPath, depName) { + const workspaceSpec = spec.slice("workspace:".length); + + if (workspaceSpec === "" || workspaceSpec === "*") return version; + if (workspaceSpec === "^") return `^${version}`; + if (workspaceSpec === "~") return `~${version}`; + + throw new Error( + `Unsupported workspace spec "${spec}" for ${depName} in ${packageJsonPath}. ` + + "Update the publish rewrite script before publishing this package.", + ); +} + +function main() { + const packageJsonPath = join(process.cwd(), "package.json"); + const pkg = loadPackageJson(packageJsonPath); + const workspacePackages = new Map( + listWorkspacePackages().map((path) => { + const workspacePkg = loadPackageJson(path); + return [workspacePkg.name, workspacePkg.version]; + }), + ); + + mkdirSync(BACKUP_DIR, { recursive: true }); + const backupPath = getBackupPath(packageJsonPath); + try { + readFileSync(backupPath, "utf8"); + } catch { + writeFileSync(backupPath, `${JSON.stringify(pkg, null, 2)}\n`); + } + + let rewrites = 0; + + for (const field of DEP_FIELDS) { + const deps = pkg[field]; + if (!deps) continue; + + for (const [depName, spec] of Object.entries(deps)) { + if (!String(spec).startsWith("workspace:")) continue; + + const depVersion = workspacePackages.get(depName); + if (!depVersion) { + throw new Error(`Cannot resolve workspace dependency ${depName} in ${packageJsonPath}`); + } + + const resolvedSpec = resolveWorkspaceSpec(spec, depVersion, packageJsonPath, depName); + deps[depName] = resolvedSpec; + rewrites += 1; + console.log(`${pkg.name} ${field} ${depName}: ${spec} -> ${resolvedSpec}`); + } + } + + if (rewrites > 0) { + writeFileSync(packageJsonPath, `${JSON.stringify(pkg, null, 2)}\n`); + } + + console.log(`Prepared ${pkg.name} for packing by rewriting ${rewrites} workspace reference(s).`); +} + +main(); diff --git a/scripts/prepare-publish-manifests.ts b/scripts/prepare-publish-manifests.ts deleted file mode 100644 index 5db90af852..0000000000 --- a/scripts/prepare-publish-manifests.ts +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env tsx - -import { readdirSync, readFileSync, writeFileSync } from "fs"; -import { join } from "path"; - -const ROOT = join(import.meta.dirname, ".."); -const PACKAGES_DIR = join(ROOT, "packages"); -const DEP_FIELDS = [ - "dependencies", - "devDependencies", - "peerDependencies", - "optionalDependencies", -] as const; - -type DepField = (typeof DEP_FIELDS)[number]; -type PackageJson = { - name: string; - version: string; - [key: string]: unknown; -} & Partial>>; - -function listPackageJsonPaths() { - return readdirSync(PACKAGES_DIR).map((dir) => join(PACKAGES_DIR, dir, "package.json")); -} - -function loadPackageJson(path: string): PackageJson { - return JSON.parse(readFileSync(path, "utf8")) as PackageJson; -} - -function resolveWorkspaceSpec(spec: string, version: string) { - const workspaceSpec = spec.slice("workspace:".length); - - if (workspaceSpec === "" || workspaceSpec === "*") return version; - if (workspaceSpec === "^") return `^${version}`; - if (workspaceSpec === "~") return `~${version}`; - if (workspaceSpec.startsWith("^")) return `^${version}`; - if (workspaceSpec.startsWith("~")) return `~${version}`; - - return workspaceSpec; -} - -function main() { - const packageJsonPaths = listPackageJsonPaths(); - const packageVersions = new Map( - packageJsonPaths.map((path) => { - const pkg = loadPackageJson(path); - return [pkg.name, pkg.version] as const; - }), - ); - - let rewrites = 0; - - for (const path of packageJsonPaths) { - const pkg = loadPackageJson(path); - let changed = false; - - for (const field of DEP_FIELDS) { - const deps = pkg[field]; - if (!deps) continue; - - for (const [depName, spec] of Object.entries(deps)) { - if (!spec.startsWith("workspace:")) continue; - - const depVersion = packageVersions.get(depName); - if (!depVersion) { - throw new Error(`Cannot resolve workspace dependency ${depName} in ${path}`); - } - - const resolvedSpec = resolveWorkspaceSpec(spec, depVersion); - if (resolvedSpec === spec) continue; - - deps[depName] = resolvedSpec; - rewrites += 1; - changed = true; - console.log(`${pkg.name} ${field} ${depName}: ${spec} -> ${resolvedSpec}`); - } - } - - if (changed) { - writeFileSync(path, `${JSON.stringify(pkg, null, 2)}\n`); - } - } - - console.log(`Rewrote ${rewrites} workspace dependency reference(s) for publish.`); -} - -main(); diff --git a/scripts/restore-package-manifest-after-pack.mjs b/scripts/restore-package-manifest-after-pack.mjs new file mode 100644 index 0000000000..c2a2cbf34e --- /dev/null +++ b/scripts/restore-package-manifest-after-pack.mjs @@ -0,0 +1,32 @@ +#!/usr/bin/env node + +import { createHash } from "node:crypto"; +import { readFileSync, rmSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { tmpdir } from "node:os"; + +const BACKUP_DIR = join(tmpdir(), "hyperframes-publish-manifest-backups"); + +function getBackupPath(packageJsonPath) { + const digest = createHash("sha256").update(packageJsonPath).digest("hex"); + return join(BACKUP_DIR, `${digest}.json`); +} + +function main() { + const packageJsonPath = join(process.cwd(), "package.json"); + const backupPath = getBackupPath(packageJsonPath); + + let backup; + try { + backup = readFileSync(backupPath, "utf8"); + } catch { + console.log(`No publish-manifest backup found for ${packageJsonPath}; nothing to restore.`); + return; + } + + writeFileSync(packageJsonPath, backup); + rmSync(backupPath, { force: true }); + console.log(`Restored ${packageJsonPath} after packing.`); +} + +main(); diff --git a/scripts/verify-packed-manifests.mjs b/scripts/verify-packed-manifests.mjs new file mode 100644 index 0000000000..057196d351 --- /dev/null +++ b/scripts/verify-packed-manifests.mjs @@ -0,0 +1,73 @@ +#!/usr/bin/env node + +import { execFileSync } from "node:child_process"; +import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs"; +import { join } from "node:path"; + +const ROOT = join(import.meta.dirname, ".."); +const PACKAGES_DIR = join(ROOT, "packages"); +const DEP_FIELDS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"]; + +function listWorkspacePackageDirs() { + return readdirSync(PACKAGES_DIR) + .map((dir) => join("packages", dir)) + .filter((dir) => existsSync(join(ROOT, dir, "package.json"))); +} + +function listWorkspaceRefs(pkg) { + const refs = []; + + for (const field of DEP_FIELDS) { + for (const [depName, spec] of Object.entries(pkg[field] || {})) { + if (String(spec).startsWith("workspace:")) { + refs.push(`${field}:${depName}=${spec}`); + } + } + } + + return refs; +} + +function parsePackJson(output, workspace) { + const match = output.match(/(\[\s*{[\s\S]*}\s*])\s*$/); + if (!match) { + throw new Error(`Could not parse npm pack JSON output for ${workspace}`); + } + + return JSON.parse(match[1]); +} + +function main() { + for (const workspace of listWorkspacePackageDirs()) { + const sourcePackageJson = JSON.parse( + readFileSync(join(ROOT, workspace, "package.json"), "utf8"), + ); + if (listWorkspaceRefs(sourcePackageJson).length === 0) continue; + + const packOutput = execFileSync("npm", ["pack", "--json", "--workspace", workspace], { + cwd: ROOT, + encoding: "utf8", + }); + const [{ filename }] = parsePackJson(packOutput, workspace); + + try { + const packedPackageJson = execFileSync("tar", ["-xOf", filename, "package/package.json"], { + cwd: ROOT, + encoding: "utf8", + }); + const packedRefs = listWorkspaceRefs(JSON.parse(packedPackageJson)); + + if (packedRefs.length > 0) { + throw new Error( + `Packed manifest for ${workspace} still contains workspace refs: ${packedRefs.join(", ")}`, + ); + } + + console.log(`Verified ${workspace}: packed manifest is publish-safe.`); + } finally { + rmSync(join(ROOT, filename), { force: true }); + } + } +} + +main();