|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// SPDX-FileCopyrightText: 2026 Aleksandr Mezin <mezin.alexander@gmail.com> |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: CC0-1.0 |
| 6 | + |
| 7 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 8 | + |
| 9 | +import { ArgumentParser } from 'argparse'; |
| 10 | +import MarkdownIt from 'markdown-it'; |
| 11 | + |
| 12 | +class ChangelogError extends Error { |
| 13 | + constructor(message, line = 0, details = {}) { |
| 14 | + super(message); |
| 15 | + |
| 16 | + this.name = 'ChangelogError'; |
| 17 | + this.line = line; |
| 18 | + this.details = details; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function plainText(token) { |
| 23 | + if (token.children) |
| 24 | + return token.children.map(child => plainText(child)).join(''); |
| 25 | + |
| 26 | + return token.content ?? ''; |
| 27 | +} |
| 28 | + |
| 29 | +function* parse(changelog) { |
| 30 | + const lines = changelog.split('\n'); |
| 31 | + let tokens = new MarkdownIt().parse(changelog, {}); |
| 32 | + let index = |
| 33 | + tokens.findIndex(token => token.tag === 'h2' && token.type === 'heading_open'); |
| 34 | + |
| 35 | + while (index >= 0) { |
| 36 | + const openToken = tokens[index++]; |
| 37 | + const inlineContainerToken = tokens[index++]; |
| 38 | + const closeToken = tokens[index++]; |
| 39 | + |
| 40 | + console.assert(closeToken.type === 'heading_close'); |
| 41 | + console.assert(closeToken.tag === openToken.tag); |
| 42 | + |
| 43 | + const header = plainText(inlineContainerToken); |
| 44 | + const [, version, date] = /^\s*(\S+)\s*-?\s*(\S+)?/.exec(header); |
| 45 | + |
| 46 | + tokens = tokens.slice(index); |
| 47 | + index = |
| 48 | + tokens.findIndex(token => token.tag === 'h2' && token.type === 'heading_open'); |
| 49 | + |
| 50 | + const start = openToken.map[0]; |
| 51 | + const end = tokens[index]?.map[0] ?? lines.length; |
| 52 | + const content = lines.slice(start, end); |
| 53 | + |
| 54 | + yield { start, end, header, version, date, content: content.join('\n') }; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function run({ input, check_version, check_date, ignore_unreleased, print_entry, output }) { |
| 59 | + const changelog = readFileSync(input, 'utf8'); |
| 60 | + const parser = parse(changelog); |
| 61 | + |
| 62 | + let { value: entry, done } = parser.next(); |
| 63 | + |
| 64 | + if (done) |
| 65 | + throw new ChangelogError('Found no version entries'); |
| 66 | + |
| 67 | + if (ignore_unreleased && entry.date === undefined && /\bunreleased\b/i.test(entry.version)) |
| 68 | + ({ value: entry, done } = parser.next()); |
| 69 | + |
| 70 | + if (done) |
| 71 | + throw new ChangelogError('Found no released versions'); |
| 72 | + |
| 73 | + if (check_version !== undefined && entry.version !== check_version) { |
| 74 | + throw new ChangelogError( |
| 75 | + `Latest changelog entry does not match the expected version "${check_version}"`, |
| 76 | + entry.start, |
| 77 | + entry |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + if (check_date !== undefined && entry.date !== check_date) { |
| 82 | + throw new ChangelogError( |
| 83 | + `Latest changelog entry does not match the expected date "${check_date}"`, |
| 84 | + entry.start, |
| 85 | + entry |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + if (print_entry === undefined) |
| 90 | + return; |
| 91 | + |
| 92 | + while (!done) { |
| 93 | + if (entry.version === print_entry) |
| 94 | + break; |
| 95 | + |
| 96 | + ({ value: entry, done } = parser.next()); |
| 97 | + } |
| 98 | + |
| 99 | + if (done) |
| 100 | + throw new ChangelogError(`Version "${print_entry}" not found`); |
| 101 | + |
| 102 | + if (!output || output === '-') |
| 103 | + process.stdout.write(entry.content); |
| 104 | + else |
| 105 | + writeFileSync(output, entry.content, 'utf8'); |
| 106 | +} |
| 107 | + |
| 108 | +function normalizeVersion(version) { |
| 109 | + version = version.replace(/^[vV]/, ''); |
| 110 | + |
| 111 | + if (!version) |
| 112 | + throw new Error('Failed to parse version'); |
| 113 | + |
| 114 | + return version; |
| 115 | +} |
| 116 | + |
| 117 | +function main() { |
| 118 | + const parser = new ArgumentParser(); |
| 119 | + |
| 120 | + parser.add_argument('-i', '--input', { |
| 121 | + default: 'CHANGELOG.md', |
| 122 | + help: 'Input file', |
| 123 | + }); |
| 124 | + |
| 125 | + parser.add_argument('-v', '--check-version', { |
| 126 | + type: normalizeVersion, |
| 127 | + help: 'Check that latest changelog entry matches the specified version', |
| 128 | + }); |
| 129 | + |
| 130 | + parser.add_argument('-d', '--check-date', { |
| 131 | + help: 'Check that latest changelog entry has the specified date', |
| 132 | + }); |
| 133 | + |
| 134 | + parser.add_argument('-u', '--ignore-unreleased', { |
| 135 | + action: 'store_true', |
| 136 | + help: 'If the latest entry is "Unreleased", ignore it and check the next entry', |
| 137 | + }); |
| 138 | + |
| 139 | + parser.add_argument('-p', '--print-entry', { |
| 140 | + type: normalizeVersion, |
| 141 | + help: 'Print changelog entry for the specified version', |
| 142 | + }); |
| 143 | + |
| 144 | + parser.add_argument('-o', '--output', { |
| 145 | + default: '-', |
| 146 | + help: 'Output file', |
| 147 | + }); |
| 148 | + |
| 149 | + run(parser.parse_args()); |
| 150 | +} |
| 151 | + |
| 152 | +main(); |
0 commit comments