@@ -4,7 +4,8 @@ import path = require('path');
44import minimist = require( 'minimist' ) ;
55import gulpUtils = require( 'gulp-util' ) ;
66
7- let changelog = require ( 'gulp-conventional-changelog' ) ;
7+ const changelog = require ( 'gulp-conventional-changelog' ) ;
8+ const semver = require ( 'semver' ) ;
89
910const args = minimist ( process . argv . slice ( 2 ) ) ;
1011const chalk = gulpUtils . colors ;
@@ -19,31 +20,33 @@ const VERSION = args['version'] || require(path.join(ROOT,'package.json')).versi
1920 */
2021task ( 'changelog' , function ( ) {
2122
22- var changelogPath = path . join ( ROOT , 'CHANGELOG.md' ) ;
23- var previousTag = getLatestTag ( ) ;
24- var currentTag = 'v' + VERSION ;
25- var contextOptions = {
23+ const changelogPath = path . join ( ROOT , 'CHANGELOG.md' ) ;
24+ const previousTag = getLatestTag ( ) ;
25+ const currentTag = 'v' + VERSION ;
26+ const fromSHA = SHA || previousTag . sha ;
27+ const contextOptions = {
2628 version : VERSION ,
2729 previousTag : previousTag . name ,
2830 currentTag : currentTag
2931 } ;
32+
3033 /* Validate different fork points for the changelog generation */
3134 if ( previousTag . name === currentTag && ! SHA ) {
3235 log ( chalk . yellow ( 'Warning: You are generating a changelog by comparing the same versions.' ) ) ;
3336 } else if ( SHA ) {
3437 log ( 'Generating changelog from commit ' + getShortSha ( SHA ) + '...' ) ;
3538 } else {
36- var shortSha = getShortSha ( previousTag . sha ) ;
39+ let shortSha = getShortSha ( previousTag . sha ) ;
3740 log ( 'Generating changelog from tag ' + previousTag . name + ' (' + shortSha + ')' ) ;
3841 }
3942
40- return src ( changelogPath , {
41- buffer : false
42- } ) . pipe ( changelog ( {
43- preset : 'angular'
44- } , contextOptions , {
45- from : SHA || previousTag . sha
46- } ) . pipe ( dest ( ROOT ) ) ) ;
43+ return src ( changelogPath )
44+ . pipe ( changelog ( { preset : 'angular' } , contextOptions , {
45+ from : fromSHA
46+ } , { } , {
47+ generateOn : _shouldGenerate
48+ } ) )
49+ . pipe ( dest ( ROOT ) ) ;
4750
4851} ) ;
4952
@@ -68,3 +71,17 @@ function getLatestTag() {
6871function getShortSha ( sha ) {
6972 return sha . substring ( 0 , 7 ) ;
7073}
74+
75+ /**
76+ * Function which determines whether the conventional-changelog should create
77+ * a new section in the CHANGELOG or not.
78+ *
79+ * - If a SHA is specified, the first checked SHA will be used to generate a new section.
80+ * - By default it just checks if the commit is tagged and if the version is valid.
81+ *
82+ * @param {Object= } commit Parsed commit from the conventional-changelog-parser.
83+ */
84+ let _isGenerated = 0 ;
85+ function _shouldGenerate ( commit ) {
86+ return SHA ? _isGenerated ++ === 0 : semver . valid ( commit . version ) ;
87+ }
0 commit comments