@@ -17,7 +17,7 @@ limitations under the License.
17
17
*/
18
18
19
19
import log from 'loglevel' ;
20
- import yargs from 'yargs/yargs ' ;
20
+ import yargs from 'yargs' ;
21
21
import { hideBin } from 'yargs/helpers' ;
22
22
import clc from 'cli-color' ;
23
23
import semver from 'semver' ;
@@ -31,7 +31,7 @@ import {
31
31
import { getLatestRelease , getReleaseBefore , getReleases , releasesContains } from "./releases" ;
32
32
import { ChangesByProject , getPackageJsonAtVersion , Project , branchExists , BranchMode } from './projects' ;
33
33
import { formatIssue } from './issue' ;
34
- import { updateChangelog } from './changelog' ;
34
+ import { previewChangelog , updateChangelog } from './changelog' ;
35
35
import { Octokit } from '@octokit/rest' ;
36
36
37
37
function formatChangeType ( changeType : ChangeType ) {
@@ -74,21 +74,35 @@ function printChangeStatus(change: IChange, projectName: string, owner: string,
74
74
}
75
75
76
76
async function main ( ) {
77
- const args = yargs ( hideBin ( process . argv ) ) . option ( 'debug' , {
78
- alias : 'd' ,
79
- type : 'boolean' ,
80
- description : "Enable debug mode" ,
81
- } ) . option ( 'check' , {
82
- type : 'boolean' ,
83
- description : "Don't update changelog, just output information on what changes would be included" ,
84
- } ) . help ( ) . usage ( "Usage: $0 [-d] [--check] <version>" ) . argv ;
85
-
86
- if ( args . _ . length !== 1 && ! args . check ) {
77
+ const args = yargs ( hideBin ( process . argv ) ) . version ( false ) . options ( {
78
+ "debug" : {
79
+ alias : 'd' ,
80
+ type : 'boolean' ,
81
+ description : "Enable debug mode" ,
82
+ } ,
83
+ "check" : {
84
+ type : 'boolean' ,
85
+ description : "Don't update changelog, just output information on what changes would be included" ,
86
+ conflicts : [ "preview" ] ,
87
+ } ,
88
+ "preview" : {
89
+ type : "boolean" ,
90
+ description : "Generate changelog as normal, but without version header and output to STDOUT." ,
91
+ conflicts : [ "check" ] ,
92
+ } ,
93
+ } ) . command ( "* [version]" , "Generate changelog for the given version" , yargs => (
94
+ yargs . positional ( "version" , {
95
+ description : "The version to generate the changelog for, " +
96
+ "required if --check and/or --preview are not specified." ,
97
+ type : "string" ,
98
+ } )
99
+ ) ) . help ( ) . parseSync ( ) ;
100
+
101
+ if ( ! args . version && ! args . check && ! args . preview ) {
87
102
// Surely yargs should be able to do this? It seems incredibly confusing and I already regret using it
88
103
console . log ( "No version specified" ) ;
89
104
return ;
90
105
}
91
- const targetRelease = args . _ [ 0 ] as string ;
92
106
93
107
if ( args . debug ) {
94
108
log . setLevel ( log . levels . DEBUG ) ;
@@ -109,26 +123,33 @@ async function main() {
109
123
let fromVer : string ;
110
124
let toVer : string ;
111
125
112
- if ( targetRelease ) {
113
- const targetReleaseSemVer = semver . parse ( targetRelease ) ;
126
+ if ( args . version ) {
127
+ const targetReleaseSemVer = semver . parse ( args . version ) ;
114
128
const targetIsPrerelease = targetReleaseSemVer . prerelease . length > 0 ;
115
129
const toVerReleaseBranch =
116
130
`release-v${ targetReleaseSemVer . major } .${ targetReleaseSemVer . minor } .${ targetReleaseSemVer . patch } ` ;
117
- if ( releasesContains ( rels , targetRelease ) ) {
118
- log . debug ( "Found existing release for " + targetRelease ) ;
131
+ if ( releasesContains ( rels , args . version ) ) {
132
+ log . debug ( "Found existing release for " + args . version ) ;
119
133
// nb. getReleases only gets the most recent 100 so this won't work
120
134
// for older releases
121
- fromVer = getReleaseBefore ( rels , targetRelease , targetIsPrerelease ) . name ;
122
- toVer = targetRelease ;
123
- } else if ( targetRelease !== 'develop' && await branchExists ( dir , toVerReleaseBranch ) ) {
124
- log . debug ( "Found release branch for " + targetRelease ) ;
135
+ fromVer = getReleaseBefore ( rels , args . version , targetIsPrerelease ) . name ;
136
+ toVer = args . version ;
137
+ } else if ( args . version !== 'develop' && await branchExists ( dir , toVerReleaseBranch ) ) {
138
+ log . debug ( "Found release branch for " + args . version ) ;
125
139
// 'to' release has had a release branch cut but not yet a full release
126
140
// compare to the tip of the release branch
127
141
fromVer = getLatestRelease ( rels , targetIsPrerelease ) . name ;
128
142
toVer = toVerReleaseBranch ;
129
143
branchMode = BranchMode . Release ;
144
+ } else if ( args . version !== 'develop' && await branchExists ( dir , "staging" ) ) {
145
+ log . debug ( "Found release branch for " + args . version ) ;
146
+ // 'to' release has had a release branch cut but not yet a full release
147
+ // compare to the tip of the release branch
148
+ fromVer = getLatestRelease ( rels , targetIsPrerelease ) . name ;
149
+ toVer = "staging" ;
150
+ branchMode = BranchMode . Release ;
130
151
} else {
131
- log . debug ( "Found neither release nor branch for " + targetRelease ) ;
152
+ log . debug ( "Found neither release nor branch for " + args . version ) ;
132
153
// the 'to' release is an doesn't-yet-exist future release -
133
154
// compare to the tip of develop (a better piece of software
134
155
// might make this configurable...)
@@ -177,7 +198,7 @@ async function main() {
177
198
178
199
const numBreaking = allChanges . filter ( c => c . breaking ) . length ;
179
200
const numFeatures = allChanges . filter ( c => c . changeType == ChangeType . FEATURE ) . length ;
180
- let suggestedBumpType ;
201
+ let suggestedBumpType : "major" | "minor" | "patch" ;
181
202
if ( numBreaking ) {
182
203
suggestedBumpType = 'major' ;
183
204
} else if ( numFeatures ) {
@@ -193,8 +214,13 @@ async function main() {
193
214
return ;
194
215
}
195
216
196
- log . debug ( "Updating changelog entry for " + targetRelease ) ;
197
- await updateChangelog ( project , allChanges , targetRelease ) ;
217
+ if ( args . preview ) {
218
+ await previewChangelog ( project , allChanges ) ;
219
+ return ;
220
+ }
221
+
222
+ log . debug ( "Updating changelog entry for " + args . version ) ;
223
+ await updateChangelog ( project , allChanges , args . version ) ;
198
224
}
199
225
200
226
main ( ) ;
0 commit comments