5
5
* MIT License
6
6
*/
7
7
8
- /* global test, cat, rm, mv */
9
-
10
8
"use strict" ;
11
9
12
10
//------------------------------------------------------------------------------
13
11
// Requirements
14
12
//------------------------------------------------------------------------------
15
13
16
- // TODO: Update to use non-global module to avoid prototype pollution.
17
- require ( "shelljs/global" ) ;
18
-
19
14
const fs = require ( "fs" ) ,
20
15
path = require ( "path" ) ,
21
16
semver = require ( "semver" ) ,
@@ -49,6 +44,21 @@ const commitTagMap = new Map([
49
44
// /MIT/, /BSD/, /Apache/, /ISC/, /WTF/, /Public Domain/
50
45
// ];
51
46
47
+ /**
48
+ * Tests if a file exists.
49
+ * @param {string } file The path of the file to be tested.
50
+ * @returns {boolean } `true` if the specified path denotes an existing file, otherwise `false`.
51
+ */
52
+ function fileExists ( file ) {
53
+
54
+ // We can't use the `throwIfNoEntry` option with `fs.statSync`, because it's not supported in Node.js 10,
55
+ // so we check if the file exists in advance.
56
+ if ( ! fs . existsSync ( file ) ) {
57
+ return false ;
58
+ }
59
+ return fs . statSync ( file ) . isFile ( ) ;
60
+ }
61
+
52
62
/**
53
63
* Loads the package.json file from the current directory.
54
64
* @returns {void }
@@ -66,7 +76,7 @@ function getPackageInfo() {
66
76
* @private
67
77
*/
68
78
function validateSetup ( ) {
69
- if ( ! test ( "-f" , "package.json" ) ) {
79
+ if ( ! fileExists ( "package.json" ) ) {
70
80
console . error ( "Missing package.json file" ) ;
71
81
ShellOps . exit ( 1 ) ;
72
82
}
@@ -333,27 +343,27 @@ function calculateReleaseInfo(prereleaseId) {
333
343
*/
334
344
function writeChangelog ( releaseInfo ) {
335
345
336
- // get most recent two tags
346
+ // get today's date in "mmmm d, yyyy" format
337
347
const now = new Date ( ) ,
338
- timestamp = dateformat ( now , "mmmm d, yyyy" ) ;
339
-
340
- // output header
341
- ( `v${ releaseInfo . version } - ${ timestamp } \n` ) . to ( "CHANGELOG.tmp" ) ;
348
+ today = dateformat ( now , "mmmm d, yyyy" ) ;
342
349
343
- // output changelog
344
- ( `\n${ releaseInfo . rawChangelog } \n\n` ) . toEnd ( "CHANGELOG.tmp" ) ;
350
+ // output header and changelog
351
+ fs . writeFileSync (
352
+ "CHANGELOG.tmp" ,
353
+ `v${ releaseInfo . version } - ${ today } \n\n${ releaseInfo . rawChangelog } \n\n`
354
+ ) ;
345
355
346
356
// ensure there's a CHANGELOG.md file
347
- if ( ! test ( "-f" , "CHANGELOG.md" ) ) {
357
+ if ( ! fileExists ( "CHANGELOG.md" ) ) {
348
358
fs . writeFileSync ( "CHANGELOG.md" , "" ) ;
349
359
}
350
360
351
- // switch-o change-o
352
- // `cat` returns a ShellString and `fs.writeFileSync` is throwing an error saying that this must be a String.
353
- fs . writeFileSync ( "CHANGELOG.md.tmp" , cat ( "CHANGELOG.tmp" , "CHANGELOG.md" ) . toString ( ) ) ;
354
- rm ( "CHANGELOG.tmp" ) ;
355
- rm ( "CHANGELOG.md" ) ;
356
- mv ( "CHANGELOG.md.tmp" , "CHANGELOG.md" ) ;
361
+ const data = ` ${ fs . readFileSync ( "CHANGELOG.tmp" , "utf-8" ) } ${ fs . readFileSync ( "CHANGELOG.md" , "utf-8" ) } ` ;
362
+
363
+ fs . writeFileSync ( "CHANGELOG.md.tmp" , data ) ;
364
+ fs . unlinkSync ( "CHANGELOG.tmp" ) ;
365
+ fs . unlinkSync ( "CHANGELOG.md" ) ;
366
+ fs . renameSync ( "CHANGELOG.md.tmp" , "CHANGELOG.md" ) ;
357
367
}
358
368
359
369
/**
0 commit comments