feat(changelog): add CHANGELOG.md, release extraction script, and developer guidelines (#414)

* feat(changelog): add CHANGELOG.md, release extraction script, and developer guidelines

* docs(agents): simplify changelog guidelines for brevity
This commit is contained in:
Yiling-J
2026-09-18 11:57:25 +08:00
committed by GitHub
parent 76ffb51bb2
commit 3a509537fd
7 changed files with 996 additions and 6 deletions
+100
View File
@@ -0,0 +1,100 @@
import { describe, expect, it } from 'vitest'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { extractChangelog } from './extract-changelog'
describe('extractChangelog', () => {
const sampleChangelog = `# Changelog
All notable changes to this project will be documented in this file.
## [Unreleased]
### Added
- Some unreleased feature
## [1.2.0] - 2026-09-18
### Added
- New feature A
- New feature B
### Fixed
- Fixed bug 1
## [1.1.0] - 2026-09-01
### Fixed
- Fixed bug 2
## [1.0.0]
### Added
- Initial release
`
it('extracts notes for version with date header', () => {
const notes = extractChangelog(sampleChangelog, '1.2.0')
expect(notes).toBe(`### Added\n- New feature A\n- New feature B\n\n### Fixed\n- Fixed bug 1`)
})
it('extracts notes when version is prefixed with "v"', () => {
const notes = extractChangelog(sampleChangelog, 'v1.2.0')
expect(notes).toBe(`### Added\n- New feature A\n- New feature B\n\n### Fixed\n- Fixed bug 1`)
})
it('extracts notes when version header has no date', () => {
const notes = extractChangelog(sampleChangelog, '1.0.0')
expect(notes).toBe(`### Added\n- Initial release`)
})
it('extracts notes for the last release at the end of the file', () => {
const notes = extractChangelog(sampleChangelog, 'v1.0.0')
expect(notes).toBe(`### Added\n- Initial release`)
})
it('extracts notes for middle versions correctly', () => {
const notes = extractChangelog(sampleChangelog, '1.1.0')
expect(notes).toBe(`### Fixed\n- Fixed bug 2`)
})
it('extracts notes for Unreleased', () => {
const notes = extractChangelog(sampleChangelog, 'Unreleased')
expect(notes).toBe(`### Added\n- Some unreleased feature`)
})
it('returns null if the version is not found', () => {
const notes = extractChangelog(sampleChangelog, '9.9.9')
expect(notes).toBeNull()
})
it('returns null for empty or whitespace version', () => {
expect(extractChangelog(sampleChangelog, '')).toBeNull()
expect(extractChangelog(sampleChangelog, ' ')).toBeNull()
expect(extractChangelog(sampleChangelog, 'v')).toBeNull()
})
it('returns null for empty content', () => {
expect(extractChangelog('', '1.0.0')).toBeNull()
})
it('handles regex special characters safely in version', () => {
const weirdChangelog = `## [1.0.0-beta.1] - 2026-01-01\n\n- Beta note\n\n## [1.0.0]`
const notes = extractChangelog(weirdChangelog, '1.0.0-beta.1')
expect(notes).toBe('- Beta note')
})
it('successfully extracts real releases from repository CHANGELOG.md', () => {
const realChangelog = readFileSync(resolve(__dirname, '../CHANGELOG.md'), 'utf-8')
const v046 = extractChangelog(realChangelog, 'v0.4.6')
expect(v046).not.toBeNull()
expect(v046).toContain('### Added')
expect(v046).toContain('Introduce Adobe Premiere Pro UXP plugin for Shumai')
expect(v046).toContain('https://github.com/shumaiOne/shumai/pull/407')
const v001 = extractChangelog(realChangelog, '0.0.1')
expect(v001).not.toBeNull()
expect(v001).toBe('### Added\n\n- Initial release of Shumai')
})
})
+115
View File
@@ -0,0 +1,115 @@
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
/**
* Extracts the release notes for a given version from the markdown content of a changelog.
*
* @param content The full text content of CHANGELOG.md
* @param version The version string to search for (e.g. "0.4.6" or "v0.4.6")
* @returns The extracted markdown text, or null if the version is not found.
*/
export function extractChangelog(content: string, version: string): string | null {
if (!content || !version) {
return null
}
const cleanVersion = version.trim().replace(/^v/i, '')
if (!cleanVersion) {
return null
}
const escapedVersion = cleanVersion.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
const headerRegex = new RegExp(`^## \\[(?:v)?${escapedVersion}\\](?:\\s*-\\s*.*)?$`, 'm')
const match = headerRegex.exec(content)
if (!match || match.index === undefined) {
return null
}
const startIndex = match.index + match[0].length
const remainingContent = content.slice(startIndex)
// Look for the next release header starting with "## ["
const nextHeaderRegex = /^## \[/m
const nextMatch = nextHeaderRegex.exec(remainingContent)
const sectionContent =
nextMatch && nextMatch.index !== undefined
? remainingContent.slice(0, nextMatch.index)
: remainingContent
return sectionContent.trim()
}
interface CliOptions {
version: string
output?: string
changelogPath: string
}
function parseCliArgs(args: string[]): CliOptions {
let version = ''
let output: string | undefined
let changelogPath = 'CHANGELOG.md'
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg === '--output' || arg === '-o') {
output = args[++i]
} else if (arg === '--changelog' || arg === '-c') {
changelogPath = args[++i]
} else if (!arg.startsWith('-') && !version) {
version = arg
}
}
return { version, output, changelogPath }
}
export function runCli(argv: string[]): void {
const { version, output, changelogPath } = parseCliArgs(argv)
if (!version) {
console.error(
'Usage: bun run scripts/extract-changelog.ts <version> [--output <path>] [--changelog <path>]',
)
process.exit(1)
}
const resolvedChangelog = resolve(changelogPath)
if (!existsSync(resolvedChangelog)) {
console.error(`Changelog file not found at: ${resolvedChangelog}`)
process.exit(1)
}
const content = readFileSync(resolvedChangelog, 'utf-8')
const notes = extractChangelog(content, version)
if (output) {
const resolvedOutput = resolve(output)
mkdirSync(dirname(resolvedOutput), { recursive: true })
writeFileSync(resolvedOutput, notes ?? '', 'utf-8')
if (notes) {
console.log(`Extracted release notes for ${version} to ${resolvedOutput}`)
} else {
console.warn(
`No changelog entry found for version ${version}; wrote empty file to ${resolvedOutput}`,
)
}
} else {
if (notes) {
process.stdout.write(notes + '\n')
} else {
console.warn(`No changelog entry found for version ${version}`)
}
}
}
// Run CLI when invoked directly
const isDirectlyExecuted =
Boolean(import.meta.main) ||
(process.argv[1] ? resolve(process.argv[1]) === resolve(import.meta.filename ?? '') : false)
if (isDirectlyExecuted) {
runCli(process.argv.slice(2))
}
+40
View File
@@ -99,6 +99,45 @@ function updateVersions(newVersion: string) {
}
}
function updateChangelog(newVersion: string) {
const changelogFile = 'CHANGELOG.md'
if (!existsSync(changelogFile)) {
return
}
const content = readFileSync(changelogFile, 'utf-8')
if (content.includes(`## [${newVersion}]`) || content.includes(`## [v${newVersion}]`)) {
console.log(` Changelog already contains ## [${newVersion}], skipping update`)
return
}
const unreleasedRegex = /^## \[Unreleased\]/m
if (!unreleasedRegex.test(content)) {
console.warn(' Warning: No ## [Unreleased] header found in CHANGELOG.md')
return
}
const now = new Date()
const year = now.getFullYear()
const month = String(now.getMonth() + 1).padStart(2, '0')
const day = String(now.getDate()).padStart(2, '0')
const dateStr = `${year}-${month}-${day}`
const replacement = `## [Unreleased]
### Added
### Fixed
### Changed
## [${newVersion}] - ${dateStr}`
const updatedContent = content.replace(unreleasedRegex, replacement)
writeFileSync(changelogFile, updatedContent, 'utf-8')
console.log(` Updated ${changelogFile} for v${newVersion} (${dateStr})`)
}
function shellQuote(value: string) {
return `'${value.replace(/'/g, `'\\''`)}'`
}
@@ -151,6 +190,7 @@ if (bumpTypes.has(releaseTarget)) {
console.log(`Bumping version from ${currentVersion} to ${newVersion}...`)
updateVersions(newVersion)
updateChangelog(newVersion)
console.log()
// 3. Update bun.lock