@@ -10,6 +10,7 @@ import { hideBin } from 'yargs/helpers';
10
10
11
11
import { updateChangelog } from './update-changelog' ;
12
12
import { generateDiff } from './generate-diff' ;
13
+ import { createEmptyChangelog } from './init' ;
13
14
14
15
import { unreleased , Version } from './constants' ;
15
16
@@ -137,6 +138,16 @@ async function validate({
137
138
}
138
139
}
139
140
141
+ interface InitOptions {
142
+ changelogPath: string ;
143
+ repoUrl: string ;
144
+ }
145
+
146
+ async function init ( { changelogPath, repoUrl } : InitOptions ) {
147
+ const changelogContent = await createEmptyChangelog ( { repoUrl } ) ;
148
+ await saveChangelog ( changelogPath , changelogContent ) ;
149
+ }
150
+
140
151
const rootDescription = `The root project directory. This determines where we \
141
152
look for changes since the last release (defaults to the entire repository at \
142
153
the current working directory), and where the changelog path is resolved from \
@@ -149,12 +160,6 @@ function configureCommonCommandOptions(_yargs: Argv) {
149
160
description : 'The changelog file path' ,
150
161
type : 'string' ,
151
162
} )
152
- . option ( 'currentVersion' , {
153
- default : npmPackageVersion ,
154
- description :
155
- 'The current version of the project that the changelog belongs to.' ,
156
- type : 'string' ,
157
- } )
158
163
. option ( 'repo' , {
159
164
default : githubRepositoryUrl ,
160
165
description : `The GitHub repository URL` ,
@@ -178,6 +183,12 @@ async function main() {
178
183
description : `Add new changes to the current release header, rather than to the '${ unreleased } ' section.` ,
179
184
type : 'boolean' ,
180
185
} )
186
+ . option ( 'currentVersion' , {
187
+ default : npmPackageVersion ,
188
+ description :
189
+ 'The current version of the project that the changelog belongs to.' ,
190
+ type : 'string' ,
191
+ } )
181
192
. epilog ( updateEpilog ) ,
182
193
)
183
194
. command (
@@ -190,8 +201,17 @@ async function main() {
190
201
description : `Verify that the current version has a release header in the changelog` ,
191
202
type : 'boolean' ,
192
203
} )
204
+ . option ( 'currentVersion' , {
205
+ default : npmPackageVersion ,
206
+ description :
207
+ 'The current version of the project that the changelog belongs to.' ,
208
+ type : 'string' ,
209
+ } )
193
210
. epilog ( validateEpilog ) ,
194
211
)
212
+ . command ( 'init' , 'Initialize a new empty changelog' , ( _yargs ) => {
213
+ configureCommonCommandOptions ( _yargs ) ;
214
+ } )
195
215
. strict ( )
196
216
. demandCommand ( )
197
217
. help ( 'help' )
@@ -255,33 +275,45 @@ async function main() {
255
275
changelogPath = path . resolve ( projectRootDirectory , changelogFilename ) ;
256
276
}
257
277
258
- try {
259
- // eslint-disable-next-line no-bitwise
260
- await fs . access ( changelogPath , fsConstants . F_OK | fsConstants . W_OK ) ;
261
- } catch ( error ) {
262
- if ( error . code === 'ENOENT' ) {
263
- exitWithError ( `File does not exist: '${ changelogPath } '` ) ;
264
- } else {
265
- exitWithError ( `File is not writable: '${ changelogPath } '` ) ;
278
+ if ( ! argv . _ ) {
279
+ throw new Error ( 'No command provided' ) ;
280
+ }
281
+ const command = argv . _ [ 0 ] ;
282
+
283
+ if ( command !== 'init' ) {
284
+ try {
285
+ // eslint-disable-next-line no-bitwise
286
+ await fs . access ( changelogPath , fsConstants . F_OK | fsConstants . W_OK ) ;
287
+ } catch ( error ) {
288
+ if ( error . code === 'ENOENT' ) {
289
+ exitWithError ( `File does not exist: '${ changelogPath } '` ) ;
290
+ } else {
291
+ exitWithError ( `File is not writable: '${ changelogPath } '` ) ;
292
+ }
293
+ return ;
266
294
}
267
- return ;
268
295
}
269
296
270
- if ( argv . _ && argv . _ [ 0 ] === 'update' ) {
297
+ if ( command === 'update' ) {
271
298
await update ( {
272
299
changelogPath,
273
300
currentVersion,
274
301
isReleaseCandidate,
275
302
repoUrl,
276
303
projectRootDirectory,
277
304
} ) ;
278
- } else if ( argv . _ && argv . _ [ 0 ] === 'validate' ) {
305
+ } else if ( command === 'validate' ) {
279
306
await validate ( {
280
307
changelogPath,
281
308
currentVersion,
282
309
isReleaseCandidate,
283
310
repoUrl,
284
311
} ) ;
312
+ } else if ( command === 'init' ) {
313
+ await init ( {
314
+ changelogPath,
315
+ repoUrl,
316
+ } ) ;
285
317
}
286
318
}
287
319
0 commit comments