1
1
#!/usr/bin/env node
2
2
3
- 'use strict'
4
-
5
- const fs = require ( 'fs' )
6
- const path = require ( 'path' )
7
- const commitStream = require ( 'commit-stream' )
8
- const split2 = require ( 'split2' )
9
- const listStream = require ( 'list-stream' )
10
- const pkgtoId = require ( 'pkg-to-id' )
11
- const stripAnsi = require ( 'strip-ansi' )
12
- const map = require ( 'map-async' )
13
- const { commitToOutput } = require ( 'changelog-maker/commit-to-output' )
14
- const collectCommitLabels = require ( 'changelog-maker/collect-commit-labels' )
15
- const groupCommits = require ( 'changelog-maker/group-commits' )
16
- const { isReleaseCommit, toGroups } = require ( 'changelog-maker/groups' )
17
- const gitexec = require ( 'gitexec' )
18
-
3
+ import fs from 'fs'
4
+ import path from 'path'
5
+ import process from 'process'
6
+ import { pipeline as _pipeline } from 'stream'
7
+ import { promisify } from 'util'
8
+ import commitStream from 'commit-stream'
9
+ import split2 from 'split2'
10
+ import pkgtoId from 'pkg-to-id'
11
+ import minimist from 'minimist'
12
+ import { isReleaseCommit } from 'changelog-maker/groups'
13
+ import { processCommits } from 'changelog-maker/process-commits'
14
+ import gitexec from 'gitexec'
15
+
16
+ const pipeline = promisify ( _pipeline )
19
17
const pkgFile = path . join ( process . cwd ( ) , 'package.json' )
20
18
const pkgData = fs . existsSync ( pkgFile ) ? require ( pkgFile ) : { }
21
19
const pkgId = pkgtoId ( pkgData )
@@ -26,23 +24,6 @@ const ghId = {
26
24
user : pkgId . user || 'nodejs' ,
27
25
repo : pkgId . name || 'node'
28
26
}
29
- const defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
30
-
31
- const formatType = {
32
- PLAINTEXT : 'plaintext' ,
33
- MARKDOWN : 'markdown' ,
34
- SIMPLE : 'simple' ,
35
- SHA : 'sha'
36
- }
37
-
38
- const getFormat = ( argv ) => {
39
- if ( argv . format && Object . values ( formatType ) . includes ( argv . format ) ) {
40
- return argv . format
41
- } else if ( argv . simple || argv . s ) {
42
- return formatType . SIMPLE
43
- }
44
- return formatType . MARKDOWN
45
- }
46
27
47
28
function replace ( s , m ) {
48
29
Object . keys ( m ) . forEach ( function ( k ) {
@@ -51,37 +32,26 @@ function replace (s, m) {
51
32
return s
52
33
}
53
34
54
- function branchDiff ( branch1 , branch2 , options , callback ) {
35
+ export async function branchDiff ( branch1 , branch2 , options ) {
55
36
if ( ! branch1 || ! branch2 ) {
56
- return callback ( new Error ( 'Must supply two branch names to compare' ) )
37
+ throw new Error ( 'Must supply two branch names to compare' )
57
38
}
58
39
59
40
const repoPath = options . repoPath || process . cwd ( )
60
-
61
- findMergeBase ( repoPath , branch1 , branch2 , ( err , commit ) => {
62
- if ( err ) { return callback ( err ) }
63
- map (
64
- [ branch1 , branch2 ] , ( branch , callback ) => {
65
- collect ( repoPath , branch , commit , branch === branch2 && options . endRef ) . pipe ( listStream . obj ( callback ) )
66
- }
67
- , ( err , branchCommits ) => err ? callback ( err ) : diffCollected ( options , branchCommits , callback )
68
- )
69
- } )
41
+ const commit = await findMergeBase ( repoPath , branch1 , branch2 )
42
+ const branchCommits = await Promise . all ( [ branch1 , branch2 ] . map ( async ( branch ) => {
43
+ return collect ( repoPath , branch , commit , branch === branch2 && options . endRef )
44
+ } ) )
45
+ return await diffCollected ( options , branchCommits )
70
46
}
71
47
72
- function findMergeBase ( repoPath , branch1 , branch2 , callback ) {
48
+ async function findMergeBase ( repoPath , branch1 , branch2 ) {
73
49
const gitcmd = `git merge-base ${ branch1 } ${ branch2 } `
74
-
75
- gitexec . execCollect ( repoPath , gitcmd , ( err , data ) => {
76
- if ( err ) {
77
- return callback ( err )
78
- }
79
-
80
- callback ( null , data . substr ( 0 , 10 ) )
81
- } )
50
+ const data = await promisify ( gitexec . execCollect ) ( repoPath , gitcmd )
51
+ return data . substr ( 0 , 10 )
82
52
}
83
53
84
- function diffCollected ( options , branchCommits , callback ) {
54
+ async function diffCollected ( options , branchCommits ) {
85
55
function isInList ( commit ) {
86
56
return branchCommits [ 0 ] . some ( ( c ) => {
87
57
if ( commit . sha === c . sha ) { return true }
@@ -102,102 +72,55 @@ function diffCollected (options, branchCommits, callback) {
102
72
103
73
let list = branchCommits [ 1 ] . filter ( ( commit ) => ! isInList ( commit ) )
104
74
105
- collectCommitLabels ( list , ( err ) => {
106
- if ( err ) {
107
- return callback ( err )
108
- }
109
-
110
- if ( options . excludeLabels . length > 0 ) {
111
- list = list . filter ( ( commit ) => {
112
- return ! commit . labels || ! commit . labels . some ( ( label ) => {
113
- return options . excludeLabels . indexOf ( label ) >= 0
114
- } )
115
- } )
116
- }
117
-
118
- if ( options . requireLabels . length > 0 ) {
119
- list = list . filter ( ( commit ) => {
120
- return commit . labels && commit . labels . some ( ( label ) => {
121
- return options . requireLabels . indexOf ( label ) >= 0
122
- } )
75
+ if ( options . excludeLabels . length > 0 ) {
76
+ list = list . filter ( ( commit ) => {
77
+ return ! commit . labels || ! commit . labels . some ( ( label ) => {
78
+ return options . excludeLabels . indexOf ( label ) >= 0
123
79
} )
124
- }
125
-
126
- if ( options . group ) {
127
- list = groupCommits ( list )
128
- }
129
-
130
- callback ( null , list )
131
- } )
132
- }
133
-
134
- function printCommits ( list , format , reverse , commitUrl ) {
135
- if ( format === formatType . SHA ) {
136
- list = list . map ( ( commit ) => `${ commit . sha . substr ( 0 , 10 ) } ` )
137
- } else if ( format === formatType . SIMPLE ) {
138
- list = list . map ( ( commit ) => commitToOutput ( commit , formatType . SIMPLE , ghId , commitUrl ) )
139
- } else if ( format === formatType . PLAINTEXT ) {
140
- // Plaintext format implies grouping.
141
- list = groupCommits ( list )
142
-
143
- const formatted = [ ]
144
- let currentGroup
145
- for ( const commit of list ) {
146
- const commitGroup = toGroups ( commit . summary )
147
- if ( currentGroup !== commitGroup ) {
148
- formatted . push ( `${ commitGroup } :` )
149
- currentGroup = commitGroup
150
- }
151
- formatted . push ( commitToOutput ( commit , formatType . PLAINTEXT , ghId , commitUrl ) )
152
- }
153
- list = formatted
154
- } else {
155
- list = list . map ( ( commit ) => {
156
- return commitToOutput ( commit , formatType . MARKDOWN , ghId , commitUrl )
157
80
} )
158
81
}
159
82
160
- if ( reverse ) {
161
- list = list . reverse ( )
162
- }
163
-
164
- let out = list . join ( '\n' ) + '\n'
165
-
166
- if ( ! process . stdout . isTTY ) {
167
- out = stripAnsi ( out )
83
+ if ( options . requireLabels . length > 0 ) {
84
+ list = list . filter ( ( commit ) => {
85
+ return commit . labels && commit . labels . some ( ( label ) => {
86
+ return options . requireLabels . indexOf ( label ) >= 0
87
+ } )
88
+ } )
168
89
}
169
90
170
- process . stdout . write ( out )
91
+ return list
171
92
}
172
93
173
- function collect ( repoPath , branch , startCommit , endRef ) {
94
+ async function collect ( repoPath , branch , startCommit , endRef ) {
174
95
const endrefcmd = endRef && replace ( refcmd , { ref : endRef } )
175
96
const untilcmd = endRef ? replace ( commitdatecmd , { refcmd : endrefcmd } ) : ''
176
97
const _gitcmd = replace ( gitcmd , { branch, startCommit, untilcmd } )
177
98
178
- return gitexec . exec ( repoPath , _gitcmd )
179
- . pipe ( split2 ( ) )
180
- . pipe ( commitStream ( ghId . user , ghId . repo ) )
99
+ const commitList = [ ]
100
+ await pipeline (
101
+ gitexec . exec ( repoPath , _gitcmd ) ,
102
+ split2 ( ) ,
103
+ commitStream ( ghId . user , ghId . repo ) ,
104
+ async function * ( source ) {
105
+ for await ( const commit of source ) {
106
+ commitList . push ( commit )
107
+ }
108
+ } )
109
+ return commitList
181
110
}
182
111
183
- module . exports = branchDiff
184
-
185
- function main ( ) {
112
+ async function main ( ) {
186
113
const minimistConfig = {
187
114
boolean : [ 'version' , 'group' , 'patch-only' , 'simple' , 'filter-release' , 'reverse' ]
188
115
}
189
- const argv = require ( ' minimist' ) ( process . argv . slice ( 2 ) , minimistConfig )
116
+ const argv = minimist ( process . argv . slice ( 2 ) , minimistConfig )
190
117
const branch1 = argv . _ [ 0 ]
191
118
const branch2 = argv . _ [ 1 ]
192
- const reverse = argv . reverse
193
119
const group = argv . group || argv . g
194
120
const endRef = argv [ 'end-ref' ]
195
- const commitUrl = argv [ 'commit-url' ] || defaultCommitUrl
196
121
let excludeLabels = [ ]
197
122
let requireLabels = [ ]
198
123
199
- const format = getFormat ( argv )
200
-
201
124
if ( argv . version || argv . v ) {
202
125
return console . log ( `v ${ require ( './package.json' ) . version } ` )
203
126
}
@@ -227,17 +150,15 @@ function main () {
227
150
endRef
228
151
}
229
152
230
- branchDiff ( branch1 , branch2 , options , ( err , list ) => {
231
- if ( err ) { throw err }
232
-
233
- if ( argv [ 'filter-release' ] ) {
234
- list = list . filter ( ( commit ) => ! isReleaseCommit ( commit . summary ) )
235
- }
153
+ let list = await branchDiff ( branch1 , branch2 , options )
154
+ if ( argv [ 'filter-release' ] ) {
155
+ list = list . filter ( ( commit ) => ! isReleaseCommit ( commit . summary ) )
156
+ }
236
157
237
- printCommits ( list , format , reverse , commitUrl )
238
- } )
158
+ await processCommits ( argv , ghId , list )
239
159
}
240
160
241
- if ( require . main === module ) {
242
- main ( )
243
- }
161
+ main ( ) . catch ( ( err ) => {
162
+ console . error ( err )
163
+ process . exit ( 1 )
164
+ } )
0 commit comments