()
| 159 | } |
| 160 | |
| 161 | function main() { |
| 162 | const versionArg = process.argv[2]; |
| 163 | const version = versionArg || readPackageVersion(); |
| 164 | |
| 165 | const text = readFileSync(CHANGELOG_PATH, 'utf8'); |
| 166 | const parsed = parseChangelog(text); |
| 167 | |
| 168 | const unrelIdx = parsed.blocks.findIndex((b) => b.name === 'Unreleased'); |
| 169 | const verIdx = parsed.blocks.findIndex((b) => b.name === version); |
| 170 | |
| 171 | if (unrelIdx === -1) { |
| 172 | console.log(`prepare-release: no [Unreleased] block — nothing to do`); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | const unrel = parsed.blocks[unrelIdx]; |
| 177 | if (!blockHasContent(unrel.body)) { |
| 178 | console.log(`prepare-release: [Unreleased] is empty — nothing to do`); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (verIdx === -1) { |
| 183 | // Case A — promote Unreleased → [version]. |
| 184 | const today = todayUtcIsoDate(); |
| 185 | const promoted = { |
| 186 | header: `## [${version}] - ${today}`, |
| 187 | name: version, |
| 188 | date: today, |
| 189 | body: trimTrailingBlank(unrel.body).concat(['']), // single trailing blank |
| 190 | }; |
| 191 | const emptied = { |
| 192 | header: `## [Unreleased]`, |
| 193 | name: 'Unreleased', |
| 194 | date: null, |
| 195 | body: ['', ''], // two blank lines for the next round of entries |
| 196 | }; |
| 197 | parsed.blocks.splice(unrelIdx, 1, emptied, promoted); |
| 198 | const next = joinChangelog(parsed); |
| 199 | writeFileSync(CHANGELOG_PATH, appendLinkRef(next, version)); |
| 200 | console.log(`prepare-release: ${version} — renamed [Unreleased] to [${version}] - ${today}`); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | // Case B — merge Unreleased sub-sections into the existing |
| 205 | // [version] sub-sections. New sub-section headings encountered in |
| 206 | // Unreleased that don't exist in [version] get appended. |
| 207 | const ver = parsed.blocks[verIdx]; |
| 208 | const unrelSubs = splitSubsections(unrel.body); |
| 209 | const verSubs = splitSubsections(ver.body); |
| 210 | |
| 211 | let merged = 0; |
| 212 | for (const us of unrelSubs.subs) { |
| 213 | const target = verSubs.subs.find((s) => s.heading === us.heading); |
| 214 | const usBody = trimTrailingBlank(us.body); |
| 215 | if (usBody.length === 0) continue; |
| 216 | if (target) { |
| 217 | // Append Unreleased's entries to the end of the version's matching |
| 218 | // sub-section, keeping their original ordering. Insert a separating |
no test coverage detected