Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .claude/skills/bump-coana/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\`."
```

Expand Down
2 changes: 1 addition & 1 deletion .claude/skills/bump-socket-patch/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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."
```

Expand Down
41 changes: 24 additions & 17 deletions scripts/release/add-unreleased.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.'
*/

Expand All @@ -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
}

Expand All @@ -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]!
Expand All @@ -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.',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[agent] Coana’s publish workflow checks out v1.x and still passes --replace, which this parser rejects. Once this lands, a release that needs a Socket CLI bump will fail before opening the PR. Can we make the workflow handle both flag versions before merging, or temporarily accept the exact legacy Coana argument as a literal prefix?

)
}
} else {
Expand All @@ -90,17 +92,24 @@ 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 {
process.stdout.write(`[add-unreleased] ${message}\n`)
}

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
Expand All @@ -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] <entry>

--file <path> changelog to edit (default: ${DEFAULT_FILE})
--replace <regex> 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 <name> Keep a Changelog section (default: ${DEFAULT_SECTION})
--file <path> changelog to edit (default: ${DEFAULT_FILE})
--replace-prefix <text> rewrite the first bullet that starts with this text
--section <name> Keep a Changelog section (default: ${DEFAULT_SECTION})

<entry> 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.
Expand Down
25 changes: 8 additions & 17 deletions scripts/release/changelog.mts
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,14 @@ function sectionRank(section: string): number {

/**
* Write `bullet` into `body`'s `### <section>` 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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
50 changes: 37 additions & 13 deletions test/release-add-unreleased.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
[
Expand All @@ -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`.',
Expand Down Expand Up @@ -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)
})
Expand All @@ -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',
Expand All @@ -279,7 +299,7 @@ describe('parseArgs', () => {
expect(parseArgs(['A note.'])).toEqual({
entry: 'A note.',
file: 'CHANGELOG.md',
replace: undefined,
replacePrefix: undefined,
section: 'Changed',
})
})
Expand All @@ -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', () => {
Expand Down
Loading