import { readFile, writeFile } from 'node:fs/promises' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' const root = join(dirname(fileURLToPath(import.meta.url)), '..', '..') const args = process.argv.slice(2) const version = option('--version') const output = option('--output') if (!/^\d+\.\d+\.\d+$/.test(version)) fail(`Invalid stable version: ${version}`) const changelog = await readFile(join(root, 'CHANGELOG.md'), 'utf8') const escaped = version.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') const lines = changelog.split(/\r?\n/) const start = lines.findIndex((line) => new RegExp(`^## \\[${escaped}\\] - \\d{4}-\\d{2}-\\d{2}$`).test(line)) if (start === -1) fail(`CHANGELOG.md is missing the ${version} release section.`) let end = lines.length for (let index = start + 1; index < lines.length; index += 1) { if (/^##\s+/.test(lines[index])) { end = index break } } const notes = `${lines.slice(start, end).join('\n').trim()}\n` await writeFile(join(root, output), notes, 'utf8') process.stdout.write(`[release-notes] wrote ${output} for ${version}\n`) function option(name) { const index = args.indexOf(name) const value = index === -1 ? undefined : args[index + 1] if (!value || value.startsWith('--')) fail(`${name} is required.`) return value } function fail(message) { process.stderr.write(`[release-notes] ${message}\n`) process.exit(1) }