@@ -10,6 +10,23 @@ const conventionalChangelog = require('conventional-changelog');
10
10
const changelogCompare = require ( 'conventional-changelog-writer/lib/util' ) ;
11
11
const merge2 = require ( 'merge2' ) ;
12
12
13
+ /** Interface that describes a package in the changelog. */
14
+ interface ChangelogPackage {
15
+ commits : any [ ] ;
16
+ breakingChanges : any [ ] ;
17
+ }
18
+
19
+ /** Hardcoded order of packages shown in the changelog. */
20
+ const changelogPackageOrder = [
21
+ 'cdk' ,
22
+ 'material' ,
23
+ 'google-maps' ,
24
+ 'youtube-player' ,
25
+ 'material-moment-adapter' ,
26
+ 'cdk-experimental' ,
27
+ 'material-experimental' ,
28
+ ] ;
29
+
13
30
/** Prompts for a changelog release name and prepends the new changelog. */
14
31
export async function promptAndGenerateChangelog ( changelogPath : string ) {
15
32
const releaseName = await promptChangelogReleaseName ( ) ;
@@ -80,14 +97,15 @@ function createChangelogWriterOptions(changelogPath: string) {
80
97
81
98
return {
82
99
// Overwrite the changelog templates so that we can render the commits grouped
83
- // by package names.
100
+ // by package names. Templates are based on the original templates of the
101
+ // angular preset: "conventional-changelog-angular/templates".
84
102
mainTemplate : readFileSync ( join ( __dirname , 'changelog-root-template.hbs' ) , 'utf8' ) ,
85
103
commitPartial : readFileSync ( join ( __dirname , 'changelog-commit-template.hbs' ) , 'utf8' ) ,
86
104
87
105
// Specify a writer option that can be used to modify the content of a new changelog section.
88
106
// See: conventional-changelog/tree/master/packages/conventional-changelog-writer
89
107
finalizeContext : ( context : any ) => {
90
- const packageGroups : { [ packageName : string ] : any [ ] } = { } ;
108
+ const packageGroups : { [ packageName : string ] : ChangelogPackage } = { } ;
91
109
92
110
context . commitGroups . forEach ( ( group : any ) => {
93
111
group . commits . forEach ( ( commit : any ) => {
@@ -115,48 +133,67 @@ function createChangelogWriterOptions(changelogPath: string) {
115
133
// require specifying the "material" package explicitly, we can remove
116
134
// the fallback to the "material" package.
117
135
const packageName = commit . package || 'material' ;
118
- const { color , title } = getTitleAndColorOfTypeLabel ( commit . type ) ;
136
+ const type = getTypeOfCommitGroupDescription ( group . title ) ;
119
137
120
138
if ( ! packageGroups [ packageName ] ) {
121
- packageGroups [ packageName ] = [ ] ;
139
+ packageGroups [ packageName ] = { commits : [ ] , breakingChanges : [ ] } ;
122
140
}
141
+ const packageGroup = packageGroups [ packageName ] ;
123
142
124
- packageGroups [ packageName ] . push ( {
125
- typeDescription : title ,
126
- typeImageUrl : `https://img.shields.io/badge/-${ title } -${ color } ` ,
127
- ...commit
128
- } ) ;
143
+ packageGroup . breakingChanges . push ( ...commit . notes ) ;
144
+ packageGroup . commits . push ( { ...commit , type} ) ;
129
145
} ) ;
130
146
} ) ;
131
147
132
- context . packageGroups = Object . keys ( packageGroups ) . sort ( ) . map ( pkgName => {
133
- return {
134
- title : pkgName ,
135
- commits : packageGroups [ pkgName ] . sort ( commitSortFunction ) ,
136
- } ;
137
- } ) ;
148
+ context . packageGroups =
149
+ Object . keys ( packageGroups ) . sort ( preferredOrderComparator ) . map ( pkgName => {
150
+ const packageGroup = packageGroups [ pkgName ] ;
151
+ return {
152
+ title : pkgName ,
153
+ commits : packageGroup . commits . sort ( commitSortFunction ) ,
154
+ breakingChanges : packageGroup . breakingChanges ,
155
+ } ;
156
+ } ) ;
138
157
139
158
return context ;
140
159
}
141
160
} ;
142
161
}
143
162
144
- /** Gets the title and color from a commit type label. */
145
- function getTitleAndColorOfTypeLabel ( typeLabel : string ) : { title : string , color : string } {
146
- if ( typeLabel === `Features` ) {
147
- return { title : 'feature' , color : 'green' } ;
148
- } else if ( typeLabel === `Bug Fixes` ) {
149
- return { title : 'bug fix' , color : 'orange' } ;
150
- } else if ( typeLabel === `Performance Improvements` ) {
151
- return { title : 'performance' , color : 'blue' } ;
152
- } else if ( typeLabel === `Reverts` ) {
153
- return { title : 'revert' , color : 'grey' } ;
154
- } else if ( typeLabel === `Documentation` ) {
155
- return { title : 'docs' , color : 'darkgreen' } ;
156
- } else if ( typeLabel === `refactor` ) {
157
- return { title : 'refactor' , color : 'lightgrey' } ;
163
+ /**
164
+ * Comparator function that sorts a given array of strings based on the
165
+ * hardcoded changelog package order. Entries which are not hardcoded are
166
+ * sorted in alphabetical order after the hardcoded entries.
167
+ */
168
+ function preferredOrderComparator ( a : string , b : string ) : number {
169
+ const aIndex = changelogPackageOrder . indexOf ( a ) ;
170
+ const bIndex = changelogPackageOrder . indexOf ( b ) ;
171
+ // If a package name could not be found in the hardcoded order, it should be
172
+ // sorted after the hardcoded entries in alphabetical order.
173
+ if ( aIndex === - 1 ) {
174
+ return bIndex === - 1 ? a . localeCompare ( b ) : 1 ;
175
+ } else if ( bIndex === - 1 ) {
176
+ return - 1 ;
177
+ }
178
+ return aIndex - bIndex ;
179
+ }
180
+
181
+ /** Gets the type of a commit group description. */
182
+ function getTypeOfCommitGroupDescription ( description : string ) : string {
183
+ if ( description === 'Features' ) {
184
+ return 'feature' ;
185
+ } else if ( description === 'Bug Fixes' ) {
186
+ return 'bug fix' ;
187
+ } else if ( description === 'Performance Improvements' ) {
188
+ return 'performance' ;
189
+ } else if ( description === 'Reverts' ) {
190
+ return 'revert' ;
191
+ } else if ( description === 'Documentation' ) {
192
+ return 'docs' ;
193
+ } else if ( description === 'Code Refactoring' ) {
194
+ return 'refactor' ;
158
195
}
159
- return { title : typeLabel , color : 'yellow' } ;
196
+ return description . toLowerCase ( ) ;
160
197
}
161
198
162
199
/** Entry-point for generating the changelog when called through the CLI. */
0 commit comments