|
| 1 | +#!/usr/bin/env node |
| 2 | +// Keep CURRENT_STATE.md current from the release workflow. |
| 3 | +// |
| 4 | +// Run right after `changeset version`, inside the changesets "version" step, so |
| 5 | +// the edit rides the existing "version packages" PR (which gets CI and a human |
| 6 | +// merge) rather than pushing directly to a protected main. |
| 7 | +// |
| 8 | +// It maintains two machine-owned spots in CURRENT_STATE.md and nothing else: |
| 9 | +// 1. the @ocpp-debugkit/toolkit row in the Package Status Table, and |
| 10 | +// 2. an auto-managed release log delimited by RELEASE-LOG markers. |
| 11 | +// Editorial prose stays hand-written. |
| 12 | +// |
| 13 | +// The script is idempotent: a second run for the same version is a no-op. |
| 14 | +// It uses only Node built-ins, so it adds no dependency. |
| 15 | + |
| 16 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 17 | +import { fileURLToPath } from 'node:url'; |
| 18 | +import { dirname, join } from 'node:path'; |
| 19 | + |
| 20 | +const repoRoot = join(dirname(fileURLToPath(import.meta.url)), '..'); |
| 21 | +const pkgPath = join(repoRoot, 'packages/toolkit/package.json'); |
| 22 | +const changelogPath = join(repoRoot, 'packages/toolkit/CHANGELOG.md'); |
| 23 | +const statePath = join(repoRoot, 'CURRENT_STATE.md'); |
| 24 | + |
| 25 | +const LOG_START = '<!-- RELEASE-LOG:START -->'; |
| 26 | +const LOG_END = '<!-- RELEASE-LOG:END -->'; |
| 27 | +const TABLE_ROW = /(\| `@ocpp-debugkit\/toolkit` \| published \| )([^|]+?)( \|)/; |
| 28 | + |
| 29 | +/** The release date, injectable for tests; otherwise today in UTC. */ |
| 30 | +function releaseDate() { |
| 31 | + const injected = process.env.RELEASE_DATE; |
| 32 | + if (injected) return injected; |
| 33 | + return new Date().toISOString().slice(0, 10); |
| 34 | +} |
| 35 | + |
| 36 | +/** Pull the change descriptions for `version` out of the toolkit CHANGELOG. */ |
| 37 | +export function changelogSummary(changelog, version) { |
| 38 | + const lines = changelog.split('\n'); |
| 39 | + const start = lines.findIndex((l) => l.trim() === `## ${version}`); |
| 40 | + if (start === -1) return null; |
| 41 | + |
| 42 | + const descriptions = []; |
| 43 | + for (let i = start + 1; i < lines.length; i++) { |
| 44 | + if (lines[i].startsWith('## ')) break; // next version section |
| 45 | + // Changeset bullets look like "- <sha>: <text>" or "- <text>". |
| 46 | + const bullet = lines[i].match(/^- (?:[0-9a-f]{7,40}: )?(.+)$/); |
| 47 | + if (bullet) descriptions.push(bullet[1].trim()); |
| 48 | + } |
| 49 | + return descriptions.length ? descriptions.join('; ') : null; |
| 50 | +} |
| 51 | + |
| 52 | +/** Insert (or leave alone, if already present) the release-log entry. */ |
| 53 | +export function withReleaseLogEntry(state, version, entry) { |
| 54 | + const startIdx = state.indexOf(LOG_START); |
| 55 | + const endIdx = state.indexOf(LOG_END); |
| 56 | + if (startIdx === -1 || endIdx === -1 || endIdx < startIdx) { |
| 57 | + throw new Error('CURRENT_STATE.md is missing the RELEASE-LOG markers'); |
| 58 | + } |
| 59 | + // Idempotent: a line already naming this exact version stays as-is. |
| 60 | + const block = state.slice(startIdx, endIdx); |
| 61 | + if (block.includes(`\`${version}\``)) return state; |
| 62 | + |
| 63 | + const insertAt = startIdx + LOG_START.length; |
| 64 | + return state.slice(0, insertAt) + '\n' + entry + state.slice(insertAt); |
| 65 | +} |
| 66 | + |
| 67 | +/** Set the toolkit version in the Package Status Table row. */ |
| 68 | +export function withTableVersion(state, version) { |
| 69 | + if (!TABLE_ROW.test(state)) { |
| 70 | + throw new Error('CURRENT_STATE.md is missing the @ocpp-debugkit/toolkit table row'); |
| 71 | + } |
| 72 | + return state.replace(TABLE_ROW, `$1${version}$3`); |
| 73 | +} |
| 74 | + |
| 75 | +/** Apply both edits to a CURRENT_STATE.md string. Pure, for testing. */ |
| 76 | +export function updateState(state, { version, summary, date }) { |
| 77 | + const next = withTableVersion(state, version); |
| 78 | + const entry = `- \`${version}\` (${date}): ${summary}`; |
| 79 | + return withReleaseLogEntry(next, version, entry); |
| 80 | +} |
| 81 | + |
| 82 | +function main() { |
| 83 | + const version = JSON.parse(readFileSync(pkgPath, 'utf8')).version; |
| 84 | + if (!version) throw new Error('could not read the toolkit version'); |
| 85 | + |
| 86 | + const changelog = readFileSync(changelogPath, 'utf8'); |
| 87 | + const summary = changelogSummary(changelog, version) ?? 'release'; |
| 88 | + |
| 89 | + const state = readFileSync(statePath, 'utf8'); |
| 90 | + const next = updateState(state, { version, summary, date: releaseDate() }); |
| 91 | + |
| 92 | + writeFileSync(statePath, next); |
| 93 | + process.stdout.write(`CURRENT_STATE.md updated for ${version}\n`); |
| 94 | +} |
| 95 | + |
| 96 | +// Run only when invoked directly, so tests can import the pure helpers. |
| 97 | +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { |
| 98 | + main(); |
| 99 | +} |
0 commit comments