mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 23:47:33 +00:00
fix: move workspace manifest rewrite to prepack
This commit is contained in:
@@ -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();
|
||||
@@ -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<Record<DepField, Record<string, string>>>;
|
||||
|
||||
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();
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
Reference in New Issue
Block a user