Skip to content

Commit ee7953c

Browse files
committed
fix: preserve frontmatter when updating changelog
fixes absolute-version#106 Signed-off-by: Tim Knight <[email protected]>
1 parent fd1cfe9 commit ee7953c

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/lifecycles/changelog.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ function outputChangelog (args, newVersion) {
2323
createIfMissing(args)
2424
const header = args.header
2525

26-
let oldContent = args.dryRun || args.releaseCount === 0 ? '' : fs.readFileSync(args.infile, 'utf-8')
27-
const oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN)
26+
const oldContent = args.dryRun || args.releaseCount === 0 ? '' : fs.readFileSync(args.infile, 'utf-8')
27+
2828
// find the position of the last release and remove header:
29-
if (oldContentStart !== -1) {
30-
oldContent = oldContent.substring(oldContentStart)
31-
}
29+
const oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN)
30+
const oldContentBody =
31+
oldContentStart !== -1
32+
? oldContent.substring(oldContentStart)
33+
: oldContent;
34+
35+
const headerStart = oldContent.indexOf(header);
36+
const changelogFrontMatter =
37+
headerStart !== -1 || headerStart !== 0
38+
? oldContent.substring(0, headerStart)
39+
: ''
40+
3241
let content = ''
3342
const context = { version: newVersion }
3443
const changelogStream = conventionalChangelog({
@@ -48,7 +57,7 @@ function outputChangelog (args, newVersion) {
4857
changelogStream.on('end', function () {
4958
checkpoint(args, 'outputting changes to %s', [args.infile])
5059
if (args.dryRun) console.info(`\n---\n${chalk.gray(content.trim())}\n---\n`)
51-
else writeFile(args, args.infile, header + '\n' + (content + oldContent).replace(/\n+$/, '\n'))
60+
else writeFile(args, args.infile, changelogFrontMatter + header + '\n' + (content + oldContentBody).replace(/\n+$/, '\n'))
5261
return resolve()
5362
})
5463
})

0 commit comments

Comments
 (0)