11
11
// Requirements
12
12
//------------------------------------------------------------------------------
13
13
14
- var fs = require ( "fs" ) ,
14
+ const fs = require ( "fs" ) ,
15
15
path = require ( "path" ) ,
16
16
shelljs = require ( "shelljs" ) ,
17
17
semver = require ( "semver" ) ,
@@ -33,7 +33,8 @@ var fs = require("fs"),
33
33
* @private
34
34
*/
35
35
function getPackageInfo ( ) {
36
- var filePath = path . join ( process . cwd ( ) , "package.json" ) ;
36
+ const filePath = path . join ( process . cwd ( ) , "package.json" ) ;
37
+
37
38
return JSON . parse ( fs . readFileSync ( filePath , "utf8" ) ) ;
38
39
}
39
40
@@ -48,7 +49,8 @@ function validateSetup() {
48
49
ShellOps . exit ( 1 ) ;
49
50
}
50
51
51
- var pkg = getPackageInfo ( ) ;
52
+ const pkg = getPackageInfo ( ) ;
53
+
52
54
if ( ! pkg . files || pkg . files . length === 0 ) {
53
55
console . error ( "Missing 'files' property in package.json" ) ;
54
56
ShellOps . exit ( 1 ) ;
@@ -58,7 +60,7 @@ function validateSetup() {
58
60
if ( ! pkg . repository ) {
59
61
console . error ( "Missing 'repository' in package.json" ) ;
60
62
ShellOps . exit ( 1 ) ;
61
- } else if ( ! / ^ [ \w \ -] + \/ [ \w \ -] + $ / . test ( pkg . repository ) ) {
63
+ } else if ( ! / ^ [ \w - ] + \/ [ \w - ] + $ / . test ( pkg . repository ) ) {
62
64
console . error ( "The 'repository' field must be in the format 'foo/bar' in package.json" ) ;
63
65
ShellOps . exit ( 1 ) ;
64
66
}
@@ -89,14 +91,14 @@ function validateEnvironment() {
89
91
* @private
90
92
*/
91
93
function getPrereleaseVersion ( currentVersion , prereleaseId , releaseType ) {
92
- var ver = new semver . SemVer ( currentVersion ) ;
94
+ const ver = new semver . SemVer ( currentVersion ) ;
93
95
94
96
// if it's already a prerelease version
95
97
if ( ver . prerelease . length ) {
96
98
return ver . inc ( "prerelease" , prereleaseId ) . version ;
97
- } else {
98
- return ver . inc ( "pre" + releaseType , prereleaseId ) . version ;
99
99
}
100
+ return ver . inc ( `pre${ releaseType } ` , prereleaseId ) . version ;
101
+
100
102
}
101
103
102
104
/**
@@ -105,9 +107,9 @@ function getPrereleaseVersion(currentVersion, prereleaseId, releaseType) {
105
107
* @private
106
108
*/
107
109
function getVersionTags ( ) {
108
- var tags = ShellOps . execSilent ( "git tag" ) . trim ( ) . split ( "\n" ) ;
110
+ const tags = ShellOps . execSilent ( "git tag" ) . trim ( ) . split ( "\n" ) ;
109
111
110
- return tags . reduce ( function ( list , tag ) {
112
+ return tags . reduce ( ( list , tag ) => {
111
113
if ( semver . valid ( tag ) ) {
112
114
list . push ( tag ) ;
113
115
}
@@ -122,11 +124,11 @@ function getVersionTags() {
122
124
* @private
123
125
*/
124
126
function parseLogs ( logs ) {
125
- var regexp = / ^ \* ( [ 0 - 9 a - f ] { 40 } ) ( (?: ( [ a - z ] + ) : ? ) ? .* ) \( ( .* ) \) / i,
127
+ const regexp = / ^ \* ( [ 0 - 9 a - f ] { 40 } ) ( (?: ( [ a - z ] + ) : ? ) ? .* ) \( ( .* ) \) / i,
126
128
parsed = [ ] ;
127
129
128
- logs . forEach ( function ( log ) {
129
- var match = log . match ( regexp ) ;
130
+ logs . forEach ( log => {
131
+ const match = log . match ( regexp ) ;
130
132
131
133
if ( match ) {
132
134
parsed . push ( {
@@ -138,7 +140,7 @@ function parseLogs(logs) {
138
140
body : ""
139
141
} ) ;
140
142
} else if ( parsed . length ) {
141
- parsed [ parsed . length - 1 ] . body += log + "\n" ;
143
+ parsed [ parsed . length - 1 ] . body += ` ${ log } \n` ;
142
144
}
143
145
} ) ;
144
146
@@ -152,32 +154,32 @@ function parseLogs(logs) {
152
154
* @returns {Object[] } An array of parsed commit log messages.
153
155
*/
154
156
function excludeReverts ( logs ) {
155
- logs = logs . slice ( ) ;
157
+ const newLogs = logs . slice ( ) ;
156
158
157
- var revertRegex = / T h i s r e v e r t s c o m m i t ( [ 0 - 9 a - f ] { 40 } ) / ,
158
- shaIndexMap = Object . create ( null ) , // Map of commit shas to indices
159
- i , log , match , sha ;
159
+ const revertRegex = / T h i s r e v e r t s c o m m i t ( [ 0 - 9 a - f ] { 40 } ) / ,
160
+ shaIndexMap = Object . create ( null ) ; // Map of commit shas to indices
161
+ let i , log , match , sha ;
160
162
161
163
// iterate in reverse because revert commits have lower indices than the
162
164
// commits they revert
163
- for ( i = logs . length - 1 ; i >= 0 ; i -- ) {
164
- log = logs [ i ] ;
165
+ for ( i = newLogs . length - 1 ; i >= 0 ; i -- ) {
166
+ log = newLogs [ i ] ;
165
167
match = log . body . match ( revertRegex ) ;
166
168
167
169
if ( match ) {
168
170
sha = match [ 1 ] ;
169
171
170
172
// only exclude this revert if we can find the commit it reverts
171
173
if ( typeof shaIndexMap [ sha ] !== "undefined" ) {
172
- logs [ shaIndexMap [ sha ] ] = null ;
173
- logs [ i ] = null ;
174
+ newLogs [ shaIndexMap [ sha ] ] = null ;
175
+ newLogs [ i ] = null ;
174
176
}
175
177
} else {
176
178
shaIndexMap [ log . sha ] = i ;
177
179
}
178
180
}
179
181
180
- return logs . filter ( Boolean ) ;
182
+ return newLogs . filter ( Boolean ) ;
181
183
}
182
184
183
185
/**
@@ -191,9 +193,9 @@ function excludeReverts(logs) {
191
193
*/
192
194
function calculateReleaseFromGitLogs ( currentVersion , logs , prereleaseId ) {
193
195
194
- logs = excludeReverts ( parseLogs ( logs ) ) ;
196
+ const excludedLogs = excludeReverts ( parseLogs ( logs ) ) ;
195
197
196
- var changelog = { } ,
198
+ const changelog = { } ,
197
199
repository = getPackageInfo ( ) . repository ;
198
200
199
201
/**
@@ -202,19 +204,20 @@ function calculateReleaseFromGitLogs(currentVersion, logs, prereleaseId) {
202
204
* @returns {string } A line for a changelog with a link to the GitHub commit
203
205
*/
204
206
function generateChangelogLine ( log ) {
205
- return log . raw . replace ( / ^ \* ( [ 0 - 9 a - f ] { 40 } ) / , function ( _ , hash ) {
206
- return "* [`" + hash . slice ( 0 , 7 ) + "`](https://github.com/" + repository + "/commit/" + hash + ")" ;
207
- } ) ;
207
+ return log . raw . replace (
208
+ / ^ \* ( [ 0 - 9 a - f ] { 40 } ) / ,
209
+ ( _ , hash ) => `* [\`${ hash . slice ( 0 , 7 ) } \`](https://github.com/${ repository } /commit/${ hash } )`
210
+ ) ;
208
211
}
209
- var releaseInfo = {
212
+ const releaseInfo = {
210
213
version : currentVersion ,
211
214
type : "" ,
212
- changelog : changelog ,
213
- rawChangelog : logs . map ( generateChangelogLine ) . join ( "\n" )
215
+ changelog,
216
+ rawChangelog : excludedLogs . map ( generateChangelogLine ) . join ( "\n" )
214
217
} ;
215
218
216
219
// arrange change types into categories
217
- logs . forEach ( function ( log ) {
220
+ excludedLogs . forEach ( log => {
218
221
219
222
// exclude untagged (e.g. revert) commits from version calculation
220
223
if ( ! log . flag ) {
@@ -238,9 +241,9 @@ function calculateReleaseFromGitLogs(currentVersion, logs, prereleaseId) {
238
241
239
242
// increment version from current version
240
243
releaseInfo . version = (
241
- prereleaseId ?
242
- getPrereleaseVersion ( currentVersion , prereleaseId , releaseInfo . type ) :
243
- semver . inc ( currentVersion , releaseInfo . type )
244
+ prereleaseId
245
+ ? getPrereleaseVersion ( currentVersion , prereleaseId , releaseInfo . type )
246
+ : semver . inc ( currentVersion , releaseInfo . type )
244
247
) ;
245
248
246
249
return releaseInfo ;
@@ -255,14 +258,15 @@ function calculateReleaseFromGitLogs(currentVersion, logs, prereleaseId) {
255
258
function calculateReleaseInfo ( prereleaseId ) {
256
259
257
260
// get most recent tag
258
- var pkg = getPackageInfo ( ) ,
261
+ const pkg = getPackageInfo ( ) ,
259
262
tags = getVersionTags ( ) ,
260
263
lastTag = tags [ tags . length - 1 ] ,
261
- commitRange = lastTag ? lastTag + " ..HEAD" : "" ;
264
+ commitRange = lastTag ? ` ${ lastTag } ..HEAD` : "" ;
262
265
263
266
// get log statements
264
- var logs = ShellOps . execSilent ( "git log --no-merges --pretty=format:\"* %H %s (%an)%n%b\" " + commitRange ) . split ( / \n / g) ;
265
- var releaseInfo = calculateReleaseFromGitLogs ( pkg . version , logs , prereleaseId ) ;
267
+ const logs = ShellOps . execSilent ( `git log --no-merges --pretty=format:"* %H %s (%an)%n%b" ${ commitRange } ` ) . split ( / \n / g) ;
268
+ const releaseInfo = calculateReleaseFromGitLogs ( pkg . version , logs , prereleaseId ) ;
269
+
266
270
releaseInfo . repository = pkg . repository ;
267
271
return releaseInfo ;
268
272
}
@@ -277,14 +281,14 @@ function calculateReleaseInfo(prereleaseId) {
277
281
function writeChangelog ( releaseInfo ) {
278
282
279
283
// get most recent two tags
280
- var now = new Date ( ) ,
284
+ const now = new Date ( ) ,
281
285
timestamp = dateformat ( now , "mmmm d, yyyy" ) ;
282
286
283
287
// output header
284
- ( "v" + releaseInfo . version + " - " + timestamp + "\n" ) . to ( "CHANGELOG.tmp" ) ;
288
+ ( `v ${ releaseInfo . version } - ${ timestamp } \n` ) . to ( "CHANGELOG.tmp" ) ;
285
289
286
290
// output changelog
287
- ( "\n" + releaseInfo . rawChangelog + "\n" ) . toEnd ( "CHANGELOG.tmp" ) ;
291
+ ( `\n ${ releaseInfo . rawChangelog } \n` ) . toEnd ( "CHANGELOG.tmp" ) ;
288
292
289
293
// ensure there's a CHANGELOG.md file
290
294
if ( ! shelljs . test ( "-f" , "CHANGELOG.md" ) ) {
@@ -312,7 +316,7 @@ function generateRelease(prereleaseId) {
312
316
ShellOps . execSilent ( "npm test" ) ;
313
317
314
318
console . log ( "Calculating changes for release" ) ;
315
- var releaseInfo = calculateReleaseInfo ( prereleaseId ) ;
319
+ const releaseInfo = calculateReleaseInfo ( prereleaseId ) ;
316
320
317
321
console . log ( "Release is %s" , releaseInfo . version ) ;
318
322
@@ -321,17 +325,15 @@ function generateRelease(prereleaseId) {
321
325
322
326
console . log ( "Committing to git" ) ;
323
327
ShellOps . exec ( "git add CHANGELOG.md" ) ;
324
- ShellOps . exec ( " git commit -m \ "Build: changelog update for " + releaseInfo . version + "\"" ) ;
328
+ ShellOps . exec ( ` git commit -m "Build: changelog update for ${ releaseInfo . version } "` ) ;
325
329
326
330
console . log ( "Generating %s" , releaseInfo . version ) ;
327
- ShellOps . execSilent ( " npm version " + releaseInfo . version ) ;
331
+ ShellOps . execSilent ( ` npm version ${ releaseInfo . version } ` ) ;
328
332
329
333
console . log ( "Fixing line endings" ) ;
330
- getPackageInfo ( ) . files . filter ( function ( dirPath ) {
331
- return fs . lstatSync ( dirPath ) . isDirectory ( ) ;
332
- } ) . forEach ( function ( filePath ) {
334
+ getPackageInfo ( ) . files . filter ( dirPath => fs . lstatSync ( dirPath ) . isDirectory ( ) ) . forEach ( filePath => {
333
335
if ( fs . existsSync ( filePath ) ) {
334
- ShellOps . execSilent ( " linefix " + filePath ) ;
336
+ ShellOps . execSilent ( ` linefix ${ filePath } ` ) ;
335
337
}
336
338
} ) ;
337
339
@@ -346,18 +348,18 @@ function generateRelease(prereleaseId) {
346
348
*/
347
349
function publishReleaseToGitHub ( releaseInfo ) {
348
350
349
- var repoParts = releaseInfo . repository . split ( "/" ) ,
351
+ const repoParts = releaseInfo . repository . split ( "/" ) ,
350
352
gh = new GitHub ( { token : process . env . ESLINT_GITHUB_TOKEN } ) ,
351
353
repo = gh . getRepo ( repoParts [ 0 ] , repoParts [ 1 ] ) ,
352
- tag = "v" + releaseInfo . version ;
354
+ tag = `v ${ releaseInfo . version } ` ;
353
355
354
356
return repo . createRelease ( {
355
- tag_name : tag ,
357
+ tag_name : tag , // eslint-disable-line camelcase
356
358
body : releaseInfo . rawChangelog ,
357
359
prerelease : ! ! semver . prerelease ( releaseInfo . version )
358
- } ) . then ( function ( ) {
360
+ } ) . then ( ( ) => {
359
361
console . log ( "Posted release notes to GitHub" ) ;
360
- } ) . catch ( function ( ex ) {
362
+ } ) . catch ( ex => {
361
363
console . error ( "Could not post release notes to GitHub" ) ;
362
364
if ( ex . message ) {
363
365
console . error ( ex . message ) ;
@@ -373,9 +375,10 @@ function publishReleaseToGitHub(releaseInfo) {
373
375
function publishRelease ( ) {
374
376
validateSetup ( ) ;
375
377
validateEnvironment ( ) ;
376
- var releaseInfo = JSON . parse ( fs . readFileSync ( ".eslint-release-info.json" , "utf8" ) ) ;
378
+ const releaseInfo = JSON . parse ( fs . readFileSync ( ".eslint-release-info.json" , "utf8" ) ) ;
379
+
380
+ let oldNpmrcContents ;
377
381
378
- var oldNpmrcContents ;
379
382
if ( fs . existsSync ( ".npmrc" ) ) {
380
383
oldNpmrcContents = fs . readFileSync ( ".npmrc" , "utf8" ) ;
381
384
} else {
@@ -389,13 +392,14 @@ function publishRelease() {
389
392
// if there's a prerelease ID, publish under "next" tag
390
393
console . log ( "Publishing to npm" ) ;
391
394
392
- var command = "npm publish" ;
395
+ let command = "npm publish" ;
396
+
393
397
if ( semver . prerelease ( releaseInfo . version ) ) {
394
398
command += " --tag next" ;
395
399
}
396
400
397
401
if ( process . env . NPM_OTP && / ^ \d + $ / . test ( process . env . NPM_OTP ) ) {
398
- command += " --otp=" + process . env . NPM_OTP ;
402
+ command += ` --otp=${ process . env . NPM_OTP } ` ;
399
403
}
400
404
401
405
ShellOps . exec ( command ) ;
@@ -419,10 +423,10 @@ function publishRelease() {
419
423
//------------------------------------------------------------------------------
420
424
421
425
module . exports = {
422
- getPrereleaseVersion : getPrereleaseVersion ,
423
- generateRelease : generateRelease ,
424
- publishRelease : publishRelease ,
425
- calculateReleaseInfo : calculateReleaseInfo ,
426
- calculateReleaseFromGitLogs : calculateReleaseFromGitLogs ,
427
- writeChangelog : writeChangelog
426
+ getPrereleaseVersion,
427
+ generateRelease,
428
+ publishRelease,
429
+ calculateReleaseInfo,
430
+ calculateReleaseFromGitLogs,
431
+ writeChangelog
428
432
} ;
0 commit comments