|
| 1 | +import { readdir, readFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 4 | + |
| 5 | +const mainPackage = '@callstack/repack'; |
| 6 | + |
| 7 | +function changelogEntry(changelog, version) { |
| 8 | + // Changesets writes version headings at column 0 and indents every |
| 9 | + // continuation line of a changeset by two spaces, so a column-0 `## ` can |
| 10 | + // only be a version heading, even inside fenced code or after a heading |
| 11 | + // written in a changeset. |
| 12 | + const content = []; |
| 13 | + let found = false; |
| 14 | + |
| 15 | + for (const line of changelog.split(/\r?\n/)) { |
| 16 | + const heading = /^## (.+?)\s*$/.exec(line); |
| 17 | + if (heading) { |
| 18 | + if (found) break; |
| 19 | + found = heading[1] === version; |
| 20 | + } else if (found) { |
| 21 | + content.push(line); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + if (!found) throw new Error(`Missing changelog entry for ${version}`); |
| 26 | + return content.join('\n').trim(); |
| 27 | +} |
| 28 | + |
| 29 | +export function generateReleaseNotes(packages) { |
| 30 | + const main = packages.find((pkg) => pkg.name === mainPackage); |
| 31 | + if (!main) throw new Error(`Missing ${mainPackage} in the release group`); |
| 32 | + |
| 33 | + // A changeset can bump packages by different types, so list each note once |
| 34 | + // under the highest type it appears with. |
| 35 | + const types = ['Major', 'Minor', 'Patch']; |
| 36 | + const changes = new Map(); |
| 37 | + for (const pkg of packages) { |
| 38 | + if (pkg.version !== main.version) { |
| 39 | + throw new Error( |
| 40 | + `Release group version mismatch: ${pkg.name}@${pkg.version}` |
| 41 | + ); |
| 42 | + } |
| 43 | + let entry; |
| 44 | + try { |
| 45 | + entry = changelogEntry(pkg.changelog, pkg.version); |
| 46 | + } catch (error) { |
| 47 | + throw new Error(`${pkg.name}: ${error.message}`, { cause: error }); |
| 48 | + } |
| 49 | + const sections = entry.split(/^### (Major|Minor|Patch) Changes\s*$/m); |
| 50 | + for (let index = 1; index < sections.length; index += 2) { |
| 51 | + for (const item of sections[index + 1].split(/(?=^- )/m)) { |
| 52 | + const note = item.trim(); |
| 53 | + const type = sections[index]; |
| 54 | + const current = changes.get(note); |
| 55 | + if ( |
| 56 | + note && |
| 57 | + !note.startsWith('- Updated dependencies') && |
| 58 | + (!current || types.indexOf(type) < types.indexOf(current)) |
| 59 | + ) { |
| 60 | + changes.set(note, type); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return [ |
| 67 | + '## Packages', |
| 68 | + packages.map((pkg) => `- \`${pkg.name}@${pkg.version}\``).join('\n'), |
| 69 | + ...types |
| 70 | + .map((type) => [ |
| 71 | + type, |
| 72 | + [...changes].filter(([, t]) => t === type).map(([note]) => note), |
| 73 | + ]) |
| 74 | + .filter(([, notes]) => notes.length) |
| 75 | + .map(([type, notes]) => `## ${type} Changes\n\n${notes.join('\n\n')}`), |
| 76 | + ].join('\n\n'); |
| 77 | +} |
| 78 | + |
| 79 | +export async function readRelease(root) { |
| 80 | + const config = JSON.parse( |
| 81 | + await readFile(path.join(root, '.changeset/config.json'), 'utf8') |
| 82 | + ); |
| 83 | + const group = config.fixed.find((names) => names.includes(mainPackage)); |
| 84 | + if (!group) throw new Error(`Missing fixed release group for ${mainPackage}`); |
| 85 | + |
| 86 | + const manifests = await Promise.all( |
| 87 | + (await readdir(path.join(root, 'packages'))).map(async (directory) => { |
| 88 | + const dir = path.join(root, 'packages', directory); |
| 89 | + const manifest = JSON.parse( |
| 90 | + await readFile(path.join(dir, 'package.json'), 'utf8') |
| 91 | + ); |
| 92 | + return { dir, ...manifest }; |
| 93 | + }) |
| 94 | + ); |
| 95 | + const packages = await Promise.all( |
| 96 | + group.map(async (name) => { |
| 97 | + const pkg = manifests.find((manifest) => manifest.name === name); |
| 98 | + if (!pkg || pkg.private || config.ignore.includes(name)) { |
| 99 | + throw new Error( |
| 100 | + `Release group package is missing or excluded: ${name}` |
| 101 | + ); |
| 102 | + } |
| 103 | + return { |
| 104 | + name, |
| 105 | + version: pkg.version, |
| 106 | + changelog: await readFile(path.join(pkg.dir, 'CHANGELOG.md'), 'utf8'), |
| 107 | + }; |
| 108 | + }) |
| 109 | + ); |
| 110 | + return generateReleaseNotes(packages); |
| 111 | +} |
| 112 | + |
| 113 | +if ( |
| 114 | + process.argv[1] && |
| 115 | + import.meta.url === pathToFileURL(process.argv[1]).href |
| 116 | +) { |
| 117 | + try { |
| 118 | + console.log( |
| 119 | + await readRelease(fileURLToPath(new URL('../../', import.meta.url))) |
| 120 | + ); |
| 121 | + } catch (error) { |
| 122 | + console.error(error.message); |
| 123 | + process.exitCode = 1; |
| 124 | + } |
| 125 | +} |
0 commit comments