|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { readFile } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import process from "node:process"; |
| 5 | + |
| 6 | +const defaultManifestPath = "docs/release/evidence/package-manifest.json"; |
| 7 | + |
| 8 | +export const readJson = async (filePath) => |
| 9 | + JSON.parse(await readFile(filePath, "utf8")); |
| 10 | + |
| 11 | +export const npmViewPackage = ({ name, version }) => { |
| 12 | + const result = spawnSync( |
| 13 | + "npm", |
| 14 | + ["view", `${name}@${version}`, "version", "dist-tags", "--json"], |
| 15 | + { |
| 16 | + encoding: "utf8" |
| 17 | + } |
| 18 | + ); |
| 19 | + |
| 20 | + if (result.status !== 0) { |
| 21 | + const output = [result.stdout, result.stderr].filter(Boolean).join("\n"); |
| 22 | + return { |
| 23 | + error: output.trim(), |
| 24 | + exists: false |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + return { |
| 29 | + exists: true, |
| 30 | + value: JSON.parse(result.stdout) |
| 31 | + }; |
| 32 | +}; |
| 33 | + |
| 34 | +const normalizeNpmView = (viewResult) => { |
| 35 | + if (!viewResult.exists) { |
| 36 | + return { |
| 37 | + distTags: {}, |
| 38 | + published: false, |
| 39 | + version: undefined |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + distTags: viewResult.value?.["dist-tags"] ?? {}, |
| 45 | + published: true, |
| 46 | + version: viewResult.value?.version |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +export const buildNpmPublishState = async ({ |
| 51 | + distTag, |
| 52 | + manifest, |
| 53 | + npmView = npmViewPackage, |
| 54 | + version |
| 55 | +}) => { |
| 56 | + const packages = manifest.packages ?? []; |
| 57 | + |
| 58 | + if (!Array.isArray(packages) || packages.length === 0) { |
| 59 | + throw new Error("Package manifest must define at least one package."); |
| 60 | + } |
| 61 | + |
| 62 | + const entries = []; |
| 63 | + |
| 64 | + for (const packageInfo of packages) { |
| 65 | + if (!packageInfo.name) { |
| 66 | + throw new Error("Package manifest entries must include name."); |
| 67 | + } |
| 68 | + |
| 69 | + const viewResult = normalizeNpmView( |
| 70 | + await npmView({ name: packageInfo.name, version }) |
| 71 | + ); |
| 72 | + const taggedVersion = viewResult.distTags[distTag]; |
| 73 | + |
| 74 | + let status = "pass"; |
| 75 | + const expected = packageInfo.publishInBeta |
| 76 | + ? `published under ${distTag}` |
| 77 | + : "unpublished for Developer Preview"; |
| 78 | + |
| 79 | + if (packageInfo.publishInBeta && !viewResult.published) { |
| 80 | + status = "missing"; |
| 81 | + } else if (packageInfo.publishInBeta && taggedVersion !== version) { |
| 82 | + status = "wrong-dist-tag"; |
| 83 | + } else if (!packageInfo.publishInBeta && viewResult.published) { |
| 84 | + status = "unexpected-published"; |
| 85 | + } |
| 86 | + |
| 87 | + entries.push({ |
| 88 | + distTag, |
| 89 | + expected, |
| 90 | + name: packageInfo.name, |
| 91 | + publishInBeta: packageInfo.publishInBeta === true, |
| 92 | + published: viewResult.published, |
| 93 | + status, |
| 94 | + taggedVersion, |
| 95 | + version, |
| 96 | + visibleVersion: viewResult.version |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + const failures = entries.filter((entry) => entry.status !== "pass"); |
| 101 | + const missingPublishable = failures.filter((entry) => |
| 102 | + ["missing", "wrong-dist-tag"].includes(entry.status) |
| 103 | + ); |
| 104 | + const unexpectedPublished = failures.filter( |
| 105 | + (entry) => entry.status === "unexpected-published" |
| 106 | + ); |
| 107 | + |
| 108 | + return { |
| 109 | + distTag, |
| 110 | + entries, |
| 111 | + status: |
| 112 | + failures.length === 0 |
| 113 | + ? "complete" |
| 114 | + : unexpectedPublished.length > 0 |
| 115 | + ? "failed" |
| 116 | + : missingPublishable.length > 0 |
| 117 | + ? "partial" |
| 118 | + : "failed", |
| 119 | + version |
| 120 | + }; |
| 121 | +}; |
| 122 | + |
| 123 | +const formatState = (state) => { |
| 124 | + const rows = state.entries.map((entry) => { |
| 125 | + const tagText = entry.taggedVersion |
| 126 | + ? `${entry.distTag}=${entry.taggedVersion}` |
| 127 | + : `${entry.distTag}=<missing>`; |
| 128 | + const publishedText = entry.published ? "published" : "not published"; |
| 129 | + |
| 130 | + return `- ${entry.name}@${entry.version}: ${entry.status} (${publishedText}, ${tagText}; expected ${entry.expected})`; |
| 131 | + }); |
| 132 | + |
| 133 | + return [ |
| 134 | + `NPM publish state: ${state.status}`, |
| 135 | + `Version: ${state.version}`, |
| 136 | + `Dist-tag: ${state.distTag}`, |
| 137 | + ...rows |
| 138 | + ].join("\n"); |
| 139 | +}; |
| 140 | + |
| 141 | +export const main = async () => { |
| 142 | + const args = new Set(process.argv.slice(2)); |
| 143 | + const repoRoot = process.cwd(); |
| 144 | + const packageJson = await readJson(path.join(repoRoot, "package.json")); |
| 145 | + const manifest = await readJson(path.join(repoRoot, defaultManifestPath)); |
| 146 | + const state = await buildNpmPublishState({ |
| 147 | + distTag: manifest.distTag ?? "next", |
| 148 | + manifest, |
| 149 | + version: packageJson.version |
| 150 | + }); |
| 151 | + |
| 152 | + if (args.has("--json")) { |
| 153 | + console.log(JSON.stringify(state, null, 2)); |
| 154 | + } else { |
| 155 | + console.log(formatState(state)); |
| 156 | + } |
| 157 | + |
| 158 | + if (args.has("--strict") && state.status !== "complete") { |
| 159 | + process.exit(1); |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 164 | + main().catch((error) => { |
| 165 | + console.error(error instanceof Error ? error.message : String(error)); |
| 166 | + process.exit(1); |
| 167 | + }); |
| 168 | +} |
0 commit comments