|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const [statusPath, outputPath] = process.argv.slice(2); |
| 5 | + |
| 6 | +if (!statusPath || !outputPath) { |
| 7 | + console.error( |
| 8 | + "Usage: node .github/scripts/build-release-pr-body.mjs <status.json> <body.md>", |
| 9 | + ); |
| 10 | + process.exit(1); |
| 11 | +} |
| 12 | + |
| 13 | +const rootDir = process.cwd(); |
| 14 | +const status = JSON.parse(await fs.readFile(statusPath, "utf8")); |
| 15 | +const packagesByName = await getPackagesByName(rootDir); |
| 16 | +const changesetsById = new Map( |
| 17 | + status.changesets.map((changeset) => [changeset.id, changeset]), |
| 18 | +); |
| 19 | + |
| 20 | +const releaseBlocks = await Promise.all( |
| 21 | + status.releases.map(async (release) => { |
| 22 | + const pkg = packagesByName.get(release.name); |
| 23 | + const header = `## ${release.name}@${release.newVersion}`; |
| 24 | + |
| 25 | + if (pkg) { |
| 26 | + const changelogPath = path.join(pkg.dir, "CHANGELOG.md"); |
| 27 | + const changelog = await readOptionalFile(changelogPath); |
| 28 | + if (changelog) { |
| 29 | + const entry = getChangelogEntry(changelog, release.newVersion); |
| 30 | + if (entry) return `${header}\n\n${entry}`; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return `${header}\n\n${fallbackReleaseNotes(release, changesetsById)}`; |
| 35 | + }), |
| 36 | +); |
| 37 | + |
| 38 | +const body = [ |
| 39 | + "This PR was opened by the Changesets CLI release workflow. When you're ready to do a release, merge this and publish the Ruby gem manually. If you're not ready to do a release yet, that's fine; whenever you add more changesets to main, this PR will be updated.", |
| 40 | + "", |
| 41 | + "# Releases", |
| 42 | + ...releaseBlocks, |
| 43 | +] |
| 44 | + .join("\n") |
| 45 | + .trimEnd(); |
| 46 | + |
| 47 | +await fs.writeFile(outputPath, `${body}\n`); |
| 48 | + |
| 49 | +async function getPackagesByName(dir) { |
| 50 | + const packages = new Map(); |
| 51 | + for (const packageJsonPath of await findPackageJsonFiles(dir)) { |
| 52 | + const pkg = JSON.parse(await fs.readFile(packageJsonPath, "utf8")); |
| 53 | + if (pkg.name) { |
| 54 | + packages.set(pkg.name, { |
| 55 | + dir: path.dirname(packageJsonPath), |
| 56 | + packageJson: pkg, |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + return packages; |
| 61 | +} |
| 62 | + |
| 63 | +async function findPackageJsonFiles(dir) { |
| 64 | + const ignoredDirs = new Set([ |
| 65 | + ".changesets-checkout", |
| 66 | + ".git", |
| 67 | + "node_modules", |
| 68 | + "vendor", |
| 69 | + ]); |
| 70 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 71 | + const files = []; |
| 72 | + |
| 73 | + for (const entry of entries) { |
| 74 | + const entryPath = path.join(dir, entry.name); |
| 75 | + if (entry.isDirectory() && !ignoredDirs.has(entry.name)) { |
| 76 | + files.push(...(await findPackageJsonFiles(entryPath))); |
| 77 | + } else if (entry.isFile() && entry.name === "package.json") { |
| 78 | + files.push(entryPath); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return files; |
| 83 | +} |
| 84 | + |
| 85 | +async function readOptionalFile(filePath) { |
| 86 | + try { |
| 87 | + return await fs.readFile(filePath, "utf8"); |
| 88 | + } catch (error) { |
| 89 | + if (error?.code === "ENOENT") return undefined; |
| 90 | + throw error; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function getChangelogEntry(changelog, version) { |
| 95 | + const headingRegex = /^(#{1,6})\s(.*)$/gm; |
| 96 | + let match; |
| 97 | + let start; |
| 98 | + let depth; |
| 99 | + let end; |
| 100 | + |
| 101 | + while ((match = headingRegex.exec(changelog))) { |
| 102 | + const headingDepth = match[1].length; |
| 103 | + const headingText = match[2].trim(); |
| 104 | + |
| 105 | + if (headingText === version) { |
| 106 | + start = headingRegex.lastIndex; |
| 107 | + depth = headingDepth; |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if (start !== undefined && headingDepth === depth) { |
| 112 | + end = match.index; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (start === undefined) return undefined; |
| 118 | + return changelog.slice(start, end).trim(); |
| 119 | +} |
| 120 | + |
| 121 | +function fallbackReleaseNotes(release, changesetsById) { |
| 122 | + const changesets = release.changesets |
| 123 | + .map((id) => changesetsById.get(id)) |
| 124 | + .filter(Boolean); |
| 125 | + const sections = [ |
| 126 | + ["major", "Major Changes"], |
| 127 | + ["minor", "Minor Changes"], |
| 128 | + ["patch", "Patch Changes"], |
| 129 | + ]; |
| 130 | + |
| 131 | + return sections |
| 132 | + .map(([type, title]) => { |
| 133 | + const summaries = changesets |
| 134 | + .filter((changeset) => |
| 135 | + changeset.releases.some( |
| 136 | + (changesetRelease) => |
| 137 | + changesetRelease.name === release.name && |
| 138 | + changesetRelease.type === type, |
| 139 | + ), |
| 140 | + ) |
| 141 | + .map((changeset) => `- ${changeset.summary}`); |
| 142 | + |
| 143 | + return summaries.length > 0 |
| 144 | + ? [`### ${title}`, "", ...summaries].join("\n") |
| 145 | + : ""; |
| 146 | + }) |
| 147 | + .filter(Boolean) |
| 148 | + .join("\n\n"); |
| 149 | +} |
0 commit comments