7
7
*/
8
8
9
9
const fs = require ( 'fs' ) ;
10
- const addStream = require ( 'add-stream ' ) ;
10
+ const merge2 = require ( 'merge2 ' ) ;
11
11
const changelog = require ( 'conventional-changelog' ) ;
12
12
const spawnSync = require ( 'child_process' ) . spawnSync ;
13
13
const npmVersion = require ( '../../package.json' ) . version ;
@@ -17,7 +17,7 @@ const npmVersion = require('../../package.json').version;
17
17
* By default, it will only create the changelog from the latest tag to head and prepends it to the changelog.
18
18
*/
19
19
const isForce = process . argv . indexOf ( '--force' ) !== - 1 ;
20
- const inStream = fs . createReadStream ( 'CHANGELOG.md' ) ;
20
+ const previousChangelog = fs . createReadStream ( 'CHANGELOG.md' ) ;
21
21
const gitTags = getAvailableTags ( ) ;
22
22
23
23
// Whether the npm version is later than the most recent tag.
@@ -27,33 +27,40 @@ const currentTag = isNpmLatest ? npmVersion : gitTags[0];
27
27
// When the npm version is the latest use the most recent tag. Otherwise use the previous tag.
28
28
const previousTag = isNpmLatest ? gitTags [ 0 ] : gitTags [ 1 ] ;
29
29
30
- inStream . on ( 'error' , function ( err ) {
31
- console . error ( 'An error occurred, while reading the previous changelog file.\n' +
32
- 'If there is no previous changelog, then you should create an empty file or use the `--force` flag.\n' + err ) ;
30
+ if ( ! isForce ) {
31
+ previousChangelog . on ( 'error' , function ( err ) {
32
+ console . error ( 'An error occurred, while reading the previous changelog file.\n' +
33
+ 'If there is changelog file, you should create an empty file or use the `--force` flag.\n' + err ) ;
33
34
34
- process . exit ( 1 ) ;
35
- } ) ;
35
+ process . exit ( 1 ) ;
36
+ } ) ;
37
+ }
36
38
37
- var config = {
39
+ const config = {
38
40
preset : 'angular' ,
39
41
releaseCount : isForce ? 0 : 1
40
42
} ;
41
43
42
- var context = {
44
+ const context = {
43
45
currentTag : currentTag ,
44
46
previousTag : previousTag
45
47
} ;
46
48
47
- var stream = changelog ( config , context )
48
- . on ( 'error' , function ( err ) {
49
- console . error ( 'An error occurred while generating the changelog: ' + err ) ;
50
- } )
51
- . pipe ( ! isForce && addStream ( inStream ) || getOutputStream ( ) ) ;
49
+ let stream = changelog ( config , context ) . on ( 'error' , function ( err ) {
50
+ console . error ( 'An error occurred while generating the changelog: ' + err ) ;
51
+ } ) ;
52
+
53
+ if ( ! isForce ) {
54
+ // Append the previous changelog to the new generated one.
55
+ stream = merge2 ( stream , previousChangelog ) ;
56
+ } else {
57
+ stream . pipe ( getOutputStream ( ) )
58
+ }
52
59
53
60
// When we are pre-pending the new changelog, then we need to wait for the input stream to be ending,
54
61
// otherwise we can't write into the same destination.
55
62
if ( ! isForce ) {
56
- inStream . on ( 'end' , function ( ) {
63
+ previousChangelog . on ( 'end' , function ( ) {
57
64
stream . pipe ( getOutputStream ( ) ) ;
58
65
} ) ;
59
66
}
0 commit comments