forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-packed-manifests.mjs
More file actions
75 lines (62 loc) · 2.24 KB
/
Copy pathverify-packed-manifests.mjs
File metadata and controls
75 lines (62 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
import { execFileSync } from "node:child_process";
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync } 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 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) {
try {
const parsed = JSON.parse(output);
return Array.isArray(parsed) ? parsed : [parsed];
} catch {
throw new Error(`Could not parse pnpm pack JSON output for ${workspace}`);
}
}
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 packDir = mkdtempSync(join(tmpdir(), "hyperframes-pack-"));
const packOutput = execFileSync("pnpm", ["pack", "--json", "--pack-destination", packDir], {
cwd: join(ROOT, workspace),
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(packDir, { force: true, recursive: true });
}
}
}
main();