77
88const fs = require ( 'fs' ) ;
99const $ = require ( 'child_process' ) ;
10+ const changelog = require ( './changelog' ) ;
1011
1112const CHANGES_HEADING = '# Changes' ;
1213const DEFAULT_CHANGES_FILE = 'CHANGES.md' ;
@@ -27,11 +28,6 @@ function buildTag(options, version, pkg) {
2728 ) ;
2829}
2930
30- function parseAuthor ( author ) {
31- const m = author . match ( / [ < ( ] / ) ;
32- return m ? author . substring ( 0 , m . index ) . trim ( ) : author ;
33- }
34-
3531// Write the commit history to the changes file
3632exports . write = function ( options , callback ) {
3733 if ( typeof options === 'function' ) {
@@ -45,10 +41,11 @@ exports.write = function (options, callback) {
4541 callback = ( ) => { } ;
4642 }
4743 }
44+
4845 const changes_file = options . changes_file || DEFAULT_CHANGES_FILE ;
4946 const package_json = fs . readFileSync ( 'package.json' , 'utf8' ) ;
5047 const pkg = JSON . parse ( package_json ) ;
51- const { version, author } = pkg ;
48+ const { version } = pkg ;
5249
5350 // Get previous file content
5451 let previous ;
@@ -75,7 +72,7 @@ exports.write = function (options, callback) {
7572 if ( version_match ) {
7673 log_range = `${ buildTag ( options , version_match [ 1 ] , pkg ) } ..HEAD` ;
7774 }
78- let flags = '--format="» ' ;
75+
7976 let commits = options . commits ;
8077 if ( commits ) {
8178 if ( commits === true ) {
@@ -87,42 +84,14 @@ exports.write = function (options, callback) {
8784 return ;
8885 }
8986 }
90- commits = commits . replace ( VARIABLE_RE , ( match , key ) => pkg [ key ] ) ;
91- flags += `[\\\`%h\\\`](${ commits } /%H)« ` ;
92- }
93- flags += '%s (%an)%n%n%b" --no-merges' ;
94- let changes ;
95- try {
96- changes = $ . execSync ( `git log ${ log_range } ${ flags } ` , {
97- encoding : 'utf8'
98- } ) ;
99- } catch ( e ) {
100- process . exit ( 1 ) ;
101- return ;
10287 }
103- // Remove blanks (if no body) and indent body
104- changes = changes
105- . replace ( / \n { 3 , } / g, '\n' )
106- // Indent body with quotes:
107- . replace ( / ^ ( [ ^ » ] ) / gm, ' > $1' )
108- // Remove trainling whitespace on blank quote lines
109- . replace ( / ^ { 4 } > \n / gm, ' >\n' ) ;
11088
111- changes = changes
112- // Replace commit markers with dashes:
113- . replace ( / ^ » / gm, '-' )
114- // Replace newline markers with newlines:
115- . replace ( / « / gm, '\n' )
116- // Restore original newlines:
117- . replace ( / \n / gm, newline ) ;
118-
119- // Only mention contributors
120- if ( author ) {
121- const author_name = typeof author === 'object'
122- ? author . name
123- : parseAuthor ( author ) ;
124- changes = changes . replace ( new RegExp ( ` \\(${ author_name } \\)$` , 'gm' ) , '' ) ;
125- }
89+ const changes = changelog ( {
90+ log_range,
91+ commits,
92+ newline,
93+ pkg
94+ } ) ;
12695
12796 // Do not allow version to be added twice
12897 if ( exists ( previous , version ) ) {
@@ -142,6 +111,7 @@ exports.write = function (options, callback) {
142111 next += `${ newline } ${ remain } ` ;
143112 }
144113 fs . writeFileSync ( changes_file , next ) ;
114+
145115 callback ( null , { previous, changes_file } ) ;
146116} ;
147117
0 commit comments