Skip to content

Commit e73321d

Browse files
committed
fix(changelog): improve changelog update logic
- Handle cases with no existing header - Handle cases where new version already exists - Add more robust version detection - Improve changelog content generation - Add logging for skipped updates
1 parent 542711c commit e73321d

4 files changed

Lines changed: 37 additions & 18 deletions

File tree

dist/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Author: Taj <[email protected]>
77
* Homepage: https://github.com/taj54/universal-version-bump#readme
88
* License: MIT
9-
* Generated on Tue, 26 Aug 2025 13:13:01 GMT
9+
* Generated on Tue, 26 Aug 2025 13:20:32 GMT
1010
*/
1111
require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap
1212
/******/ var __webpack_modules__ = ({
@@ -33009,11 +33009,21 @@ class ChangelogService {
3300933009
const headerIndex = existingChangelog.indexOf(separator);
3301033010
if (headerIndex !== -1) {
3301133011
const header = existingChangelog.substring(0, headerIndex + separator.length);
33012-
const restOfChangelog = existingChangelog.substring(headerIndex + separator.length);
33013-
const newChangelog = header + changelogContent + restOfChangelog;
33014-
await this.fileHandler.writeFile(changelogPath, newChangelog);
33012+
const contentAfterHeader = existingChangelog.substring(headerIndex + separator.length);
33013+
const firstVersionIndex = contentAfterHeader.search(/## v\d+\.\d+\.\d+/);
33014+
if (firstVersionIndex !== -1) {
33015+
const oldChangelog = contentAfterHeader.substring(firstVersionIndex);
33016+
const newChangelog = header + changelogContent + oldChangelog;
33017+
await this.fileHandler.writeFile(changelogPath, newChangelog);
33018+
}
33019+
else {
33020+
// No version found after header, so just append the new changelog
33021+
const newChangelog = header + changelogContent;
33022+
await this.fileHandler.writeFile(changelogPath, newChangelog);
33023+
}
3301533024
}
3301633025
else {
33026+
// No separator found, so prepend the new changelog
3301733027
const newChangelog = changelogContent + existingChangelog;
3301833028
await this.fileHandler.writeFile(changelogPath, newChangelog);
3301933029
}

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/changelogService.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,20 @@ export class ChangelogService {
9595

9696
if (headerIndex !== -1) {
9797
const header = existingChangelog.substring(0, headerIndex + separator.length);
98-
const restOfChangelog = existingChangelog.substring(headerIndex + separator.length);
99-
const newChangelog = header + changelogContent + restOfChangelog;
100-
await this.fileHandler.writeFile(changelogPath, newChangelog);
98+
const contentAfterHeader = existingChangelog.substring(headerIndex + separator.length);
99+
const firstVersionIndex = contentAfterHeader.search(/## v\d+\.\d+\.\d+/);
100+
101+
if (firstVersionIndex !== -1) {
102+
const oldChangelog = contentAfterHeader.substring(firstVersionIndex);
103+
const newChangelog = header + changelogContent + oldChangelog;
104+
await this.fileHandler.writeFile(changelogPath, newChangelog);
105+
} else {
106+
// No version found after header, so just append the new changelog
107+
const newChangelog = header + changelogContent;
108+
await this.fileHandler.writeFile(changelogPath, newChangelog);
109+
}
101110
} else {
111+
// No separator found, so prepend the new changelog
102112
const newChangelog = changelogContent + existingChangelog;
103113
await this.fileHandler.writeFile(changelogPath, newChangelog);
104114
}

tests/services/changelogService.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,18 @@ describe('ChangelogService', () => {
6363
expect(changelog).toContain('- maintenance');
6464
});
6565

66-
it('should update the changelog file', async () => {
67-
const existingChangelogWithHeader = '# Header\n\n---\n\nexisting content';
68-
const newContent = 'new content';
69-
const expectedContent = '# Header\n\n---\n\nnew contentexisting content';
70-
71-
const readFileSpy = vi
72-
.spyOn(fileHandler, 'readFile')
73-
.mockResolvedValue(existingChangelogWithHeader);
74-
const writeFileSpy = vi.spyOn(fileHandler, 'writeFile').mockResolvedValue();
66+
it('should update the changelog file, skipping last change noted info', async () => {
67+
const existingChangelog =
68+
'# Changelog\n\n---\n\nThis is the last change noted info.\n\n## v1.0.0\n\n- Initial release';
69+
const newContent = '## v1.1.0\n\n- New feature';
70+
const expectedContent =
71+
'# Changelog\n\n---\n\n## v1.1.0\n\n- New feature## v1.0.0\n\n- Initial release';
72+
73+
vi.spyOn(fileHandler, 'readFile').mockResolvedValue(existingChangelog);
74+
const writeFileSpy = vi.spyOn(fileHandler, 'writeFile').mockResolvedValue(undefined);
7575

7676
await changelogService.updateChangelog(newContent);
7777

78-
expect(readFileSpy).toHaveBeenCalledWith('CHANGELOG.md');
7978
expect(writeFileSpy).toHaveBeenCalledWith('CHANGELOG.md', expectedContent);
8079
});
8180

0 commit comments

Comments
 (0)