@@ -23,12 +23,21 @@ function outputChangelog (args, newVersion) {
23
23
createIfMissing ( args )
24
24
const header = args . header
25
25
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
+
28
28
// 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
+
32
41
let content = ''
33
42
const context = { version : newVersion }
34
43
const changelogStream = conventionalChangelog ( {
@@ -48,7 +57,7 @@ function outputChangelog (args, newVersion) {
48
57
changelogStream . on ( 'end' , function ( ) {
49
58
checkpoint ( args , 'outputting changes to %s' , [ args . infile ] )
50
59
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' ) )
52
61
return resolve ( )
53
62
} )
54
63
} )
0 commit comments