diff --git a/.claude/skills/bump-coana/SKILL.md b/.claude/skills/bump-coana/SKILL.md index 259dfa9c7..06d5ff145 100644 --- a/.claude/skills/bump-coana/SKILL.md +++ b/.claude/skills/bump-coana/SKILL.md @@ -28,7 +28,7 @@ missing. Read [the pin and snapshot notes](references/pin-and-snapshots.md). ```sh node scripts/release/add-unreleased.mts \ - --replace '^Updated the Coana CLI to v ' \ + --replace-prefix 'Updated the Coana CLI to v ' \ "Updated the Coana CLI to v \`$ARGUMENTS\`." ``` diff --git a/.claude/skills/bump-socket-patch/SKILL.md b/.claude/skills/bump-socket-patch/SKILL.md index 791e6d9c9..5320ba646 100644 --- a/.claude/skills/bump-socket-patch/SKILL.md +++ b/.claude/skills/bump-socket-patch/SKILL.md @@ -24,7 +24,7 @@ Take the exact released version as `$ARGUMENTS`. Ask for it if missing. Read ```sh node scripts/release/add-unreleased.mts \ - --replace '^Updated @socketsecurity/socket-patch to v' \ + --replace-prefix 'Updated @socketsecurity/socket-patch to v' \ "Updated @socketsecurity/socket-patch to v $ARGUMENTS." ``` diff --git a/scripts/release/add-unreleased.mts b/scripts/release/add-unreleased.mts index f1146f5e6..100f2d339 100644 --- a/scripts/release/add-unreleased.mts +++ b/scripts/release/add-unreleased.mts @@ -12,7 +12,7 @@ * * Example — the Coana bump, which reruns on every Coana release: * node scripts/release/add-unreleased.mts \ - * --replace '^Updated the Coana CLI to v ' \ + * --replace-prefix 'Updated the Coana CLI to v ' \ * 'Updated the Coana CLI to v `15.10.28`.' */ @@ -31,7 +31,7 @@ const DEFAULT_SECTION = 'Changed' export interface AddUnreleasedArgs { readonly entry: string readonly file: string - readonly replace: RegExp | undefined + readonly replacePrefix: string | undefined readonly section: string } @@ -44,7 +44,7 @@ export interface AddUnreleasedArgs { export function parseArgs(argv: readonly string[]): AddUnreleasedArgs { const positional: string[] = [] let file = DEFAULT_FILE - let replace: RegExp | undefined + let replacePrefix: string | undefined let section = DEFAULT_SECTION for (let i = 0, { length } = argv; i < length; i += 1) { const arg = argv[i]! @@ -59,17 +59,19 @@ export function parseArgs(argv: readonly string[]): AddUnreleasedArgs { i += 1 if (arg === '--file') { file = value - } else if (arg === '--replace') { - // Unicode mode for parity with the patterns in changelog.mts. Not - // global: `addUnreleasedEntry` matches once per bullet, and a lastIndex - // carried between bullets would skip every other one. - replace = new RegExp(value, 'u') + } else if (arg === '--replace-prefix') { + if (!value.trim()) { + throw new Error( + '[add-unreleased] --replace-prefix needs nonblank text.', + ) + } + replacePrefix = value } else if (arg === '--section') { section = value } else { throw new Error( `[add-unreleased] unknown flag ${arg}.\n` + - ' Known: --file, --replace, --section.', + ' Known: --file, --replace-prefix, --section.', ) } } else { @@ -90,7 +92,7 @@ export function parseArgs(argv: readonly string[]): AddUnreleasedArgs { if (!entry) { throw new Error('[add-unreleased] the entry is empty.') } - return { entry, file, replace, section } + return { entry, file, replacePrefix, section } } function log(message: string): void { @@ -98,9 +100,16 @@ function log(message: string): void { } function main(): void { - const { entry, file, replace, section } = parseArgs(process.argv.slice(2)) + const { entry, file, replacePrefix, section } = parseArgs( + process.argv.slice(2), + ) const changelog = readFileSync(file, 'utf8') - const updated = addUnreleasedEntry({ changelog, entry, replace, section }) + const updated = addUnreleasedEntry({ + changelog, + entry, + replacePrefix, + section, + }) if (updated === changelog) { log(`${file} already reads "${entry}" under ## [Unreleased] / ${section}.`) return @@ -115,11 +124,9 @@ const SCRIPT_META: ScriptMeta = { "adds one note under CHANGELOG.md's ## [Unreleased] heading, leaving the version and the release headings to the release workflow", help: `Usage: node scripts/release/add-unreleased.mts [flags] - --file changelog to edit (default: ${DEFAULT_FILE}) - --replace rewrite the first bullet in the section whose text matches, - instead of appending a second one — for a note that reruns, - such as a dependency bump updating its own line - --section Keep a Changelog section (default: ${DEFAULT_SECTION}) + --file changelog to edit (default: ${DEFAULT_FILE}) + --replace-prefix rewrite the first bullet that starts with this text + --section Keep a Changelog section (default: ${DEFAULT_SECTION}) is the bullet's text; a leading "- " is optional. The block is created when the previous release consumed it, so there is always somewhere to write. diff --git a/scripts/release/changelog.mts b/scripts/release/changelog.mts index a14ede739..30c1fcec1 100644 --- a/scripts/release/changelog.mts +++ b/scripts/release/changelog.mts @@ -273,15 +273,14 @@ function sectionRank(section: string): number { /** * Write `bullet` into `body`'s `###
` block, creating that section in - * `SECTION_ORDER` position when it is missing. When `replace` matches the text - * of a bullet already in the section, that bullet is rewritten in place rather - * than a second one appended. + * `SECTION_ORDER` position when it is missing. Rewrite the first bullet in + * the section whose text starts with `replacePrefix`. */ function writeSectionEntry( body: readonly string[], section: string, bullet: string, - replace: RegExp | undefined, + replacePrefix: string | undefined, ): string[] { const lines = [...body] const h3s = headingLineIndexes(lines.join('\n'), 3) @@ -304,19 +303,13 @@ function writeSectionEntry( return lines } const next = h3s.find(index => index > at) ?? lines.length - if (replace) { - // A global or sticky regex carries lastIndex between calls, so a caller's - // /g would make every other test fail. Match with a stateless copy. - const matcher = - replace.global || replace.sticky - ? new RegExp(replace.source, replace.flags.replace(/[gy]/gu, '')) - : replace + if (replacePrefix) { for (let i = at + 1; i < next; i += 1) { const line = lines[i]! const text = /^\s*-\s+/.exec(line) ? line.replace(/^\s*-\s+/, '') : undefined - if (text !== undefined && matcher.test(text)) { + if (text?.startsWith(replacePrefix)) { lines[i] = bullet return lines } @@ -334,10 +327,8 @@ export interface AddUnreleasedEntryConfig { readonly changelog: string // The bullet's text, without the leading `- `. readonly entry: string - // Tested against the TEXT of each bullet already in the section (the `- ` is - // stripped first). The first match is rewritten instead of a second bullet - // being appended, so a repeated bump updates its own line. - readonly replace?: RegExp | undefined + // Literal prefix of the bullet text after stripping the leading `- `. + readonly replacePrefix?: string | undefined // Keep a Changelog section the entry belongs under. Defaults to `Changed`. readonly section?: string | undefined } @@ -377,7 +368,7 @@ export function addUnreleasedEntry(config: AddUnreleasedEntryConfig): string { lines.slice(range.start + 1, range.end), cfg.section ?? 'Changed', `- ${cfg.entry}`, - cfg.replace, + cfg.replacePrefix, ) const head = lines.slice(0, range.start) const tail = lines.slice(range.end) diff --git a/test/release-add-unreleased.test.mts b/test/release-add-unreleased.test.mts index 80dee2551..ff864e5f6 100644 --- a/test/release-add-unreleased.test.mts +++ b/test/release-add-unreleased.test.mts @@ -24,7 +24,7 @@ const RELEASED = [ '', ].join('\n') -const COANA = /^Updated the Coana CLI to v /u +const COANA_PREFIX = 'Updated the Coana CLI to v ' function build(...blocks: readonly string[]): string { return [PREAMBLE, ...blocks].join('\n') @@ -120,7 +120,7 @@ describe('addUnreleasedEntry', () => { ) }) - // A bump reruns on every upstream release. Without `replace` the block would + // A bump reruns on every upstream release. Without a prefix the block would // accumulate one stale bullet per run. it('rewrites a matching bullet in place instead of stacking', () => { const changelog = build( @@ -139,7 +139,7 @@ describe('addUnreleasedEntry', () => { const updated = addUnreleasedEntry({ changelog, entry: 'Updated the Coana CLI to v `15.10.28`.', - replace: COANA, + replacePrefix: COANA_PREFIX, }) expect(updated).not.toContain('15.10.26.') expect( @@ -148,6 +148,26 @@ describe('addUnreleasedEntry', () => { expect(updated).toContain('### Fixed\n- A bug.') }) + it('matches punctuation in the replacement prefix literally', () => { + const previousEntry = 'Updated tool [cli] v1.' + const entry = 'Updated tool [cli] v2.' + const changelog = build( + [ + '## [Unreleased]', + '', + '### Changed', + '- Updated tool c v1.', + `- ${previousEntry}`, + '', + ].join('\n'), + RELEASED, + ) + + const args = parseArgs(['--replace-prefix', 'Updated tool [cli] ', entry]) + const updated = addUnreleasedEntry({ ...args, changelog }) + expect(updated).toBe(changelog.replace(previousEntry, entry)) + }) + it('only replaces within the named section', () => { const changelog = build( [ @@ -162,7 +182,7 @@ describe('addUnreleasedEntry', () => { const updated = addUnreleasedEntry({ changelog, entry: 'Updated the Coana CLI to v `15.10.28`.', - replace: COANA, + replacePrefix: COANA_PREFIX, }) expect(updated).toContain( '### Fixed\n- Updated the Coana CLI to v `15.10.26`.', @@ -238,13 +258,13 @@ describe('addUnreleasedEntry', () => { const once = addUnreleasedEntry({ changelog: build(RELEASED), entry: 'Updated the Coana CLI to v `15.10.28`.', - replace: COANA, + replacePrefix: COANA_PREFIX, }) expect( addUnreleasedEntry({ changelog: once, entry: 'Updated the Coana CLI to v `15.10.28`.', - replace: COANA, + replacePrefix: COANA_PREFIX, }), ).toBe(once) }) @@ -254,7 +274,7 @@ describe('addUnreleasedEntry', () => { const changelog = addUnreleasedEntry({ changelog: build(RELEASED), entry: 'Updated the Coana CLI to v `15.10.28`.', - replace: COANA, + replacePrefix: COANA_PREFIX, }) const heading = changelogHeading( '1.1.163', @@ -279,7 +299,7 @@ describe('parseArgs', () => { expect(parseArgs(['A note.'])).toEqual({ entry: 'A note.', file: 'CHANGELOG.md', - replace: undefined, + replacePrefix: undefined, section: 'Changed', }) }) @@ -290,15 +310,19 @@ describe('parseArgs', () => { 'Fixed', '--file', 'docs/CHANGELOG.md', - '--replace', - '^Updated ', + '--replace-prefix', + 'Updated ', 'A note.', ]) expect(args.section).toBe('Fixed') expect(args.file).toBe('docs/CHANGELOG.md') - expect(args.replace?.source).toBe('^Updated ') - // Global would carry lastIndex between bullets and skip every other one. - expect(args.replace?.global).toBe(false) + expect(args.replacePrefix).toBe('Updated ') + }) + + it('rejects a blank replacement prefix', () => { + expect(() => parseArgs(['--replace-prefix', ' ', 'A note.'])).toThrow( + /--replace-prefix/u, + ) }) it('tolerates a leading bullet marker on the entry', () => {