@@ -13,16 +13,16 @@ const checkpoint = require('./lib/checkpoint')
13
13
const printError = require ( './lib/print-error' )
14
14
const runExec = require ( './lib/run-exec' )
15
15
const runLifecycleScript = require ( './lib/run-lifecycle-script' )
16
+ const writeFile = require ( './lib/write-file' )
16
17
17
18
module . exports = function standardVersion ( argv ) {
18
19
var pkgPath = path . resolve ( process . cwd ( ) , './package.json' )
19
20
var pkg = require ( pkgPath )
20
21
var newVersion = pkg . version
21
- var scripts = argv . scripts || { }
22
22
var defaults = require ( './defaults' )
23
23
var args = Object . assign ( { } , defaults , argv )
24
24
25
- return runLifecycleScript ( args , 'prebump' , null , scripts )
25
+ return runLifecycleScript ( args , 'prebump' , null )
26
26
. then ( ( stdout ) => {
27
27
if ( stdout && stdout . trim ( ) . length ) args . releaseAs = stdout . trim ( )
28
28
return bumpVersion ( args . releaseAs )
@@ -36,13 +36,13 @@ module.exports = function standardVersion (argv) {
36
36
checkpoint ( args , 'skip version bump on first release' , [ ] , chalk . red ( figures . cross ) )
37
37
}
38
38
39
- return runLifecycleScript ( args , 'postbump' , newVersion , scripts )
39
+ return runLifecycleScript ( args , 'postbump' , newVersion , args )
40
40
} )
41
41
. then ( ( ) => {
42
- return outputChangelog ( args )
42
+ return outputChangelog ( args , newVersion )
43
43
} )
44
44
. then ( ( ) => {
45
- return runLifecycleScript ( args , 'precommit' , newVersion , scripts )
45
+ return runLifecycleScript ( args , 'precommit' , newVersion , args )
46
46
} )
47
47
. then ( ( message ) => {
48
48
if ( message && message . length ) args . message = message
@@ -61,7 +61,7 @@ module.exports = function standardVersion (argv) {
61
61
* attempt to update the version # in a collection of common config
62
62
* files, e.g., package.json, bower.json.
63
63
*
64
- * @param argv config object
64
+ * @param args config object
65
65
* @param newVersion version # to update to.
66
66
* @return {string }
67
67
*/
@@ -78,7 +78,7 @@ function updateConfigs (args, newVersion) {
78
78
var filename = path . basename ( configPath )
79
79
checkpoint ( args , 'bumping version in ' + filename + ' from %s to %s' , [ config . version , newVersion ] )
80
80
config . version = newVersion
81
- fs . writeFileSync ( configPath , JSON . stringify ( config , null , 2 ) + '\n' , 'utf-8 ')
81
+ writeFile ( args , configPath , JSON . stringify ( config , null , 2 ) + '\n' )
82
82
// flag any config files that we modify the version # for
83
83
// as having been updated.
84
84
configsToUpdate [ configPath ] = true
@@ -171,19 +171,21 @@ function bumpVersion (releaseAs, callback) {
171
171
} )
172
172
}
173
173
174
- function outputChangelog ( argv ) {
174
+ function outputChangelog ( args , newVersion ) {
175
175
return new Promise ( ( resolve , reject ) => {
176
- createIfMissing ( argv )
176
+ createIfMissing ( args )
177
177
var header = '# Change Log\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n'
178
- var oldContent = fs . readFileSync ( argv . infile , 'utf-8' )
178
+ var oldContent = args . dryRun ? '' : fs . readFileSync ( args . infile , 'utf-8' )
179
179
// find the position of the last release and remove header:
180
180
if ( oldContent . indexOf ( '<a name=' ) !== - 1 ) {
181
181
oldContent = oldContent . substring ( oldContent . indexOf ( '<a name=' ) )
182
182
}
183
183
var content = ''
184
+ var context
185
+ if ( args . dryRun ) context = { version : newVersion }
184
186
var changelogStream = conventionalChangelog ( {
185
187
preset : 'angular'
186
- } , undefined , { merges : null } )
188
+ } , context , { merges : null } )
187
189
. on ( 'error' , function ( err ) {
188
190
return reject ( err )
189
191
} )
@@ -193,63 +195,64 @@ function outputChangelog (argv) {
193
195
} )
194
196
195
197
changelogStream . on ( 'end' , function ( ) {
196
- checkpoint ( argv , 'outputting changes to %s' , [ argv . infile ] )
197
- fs . writeFileSync ( argv . infile , header + '\n' + ( content + oldContent ) . replace ( / \n + $ / , '\n' ) , 'utf-8' )
198
+ checkpoint ( args , 'outputting changes to %s' , [ args . infile ] )
199
+ if ( args . dryRun ) console . info ( `\n---\n${ chalk . gray ( content . trim ( ) ) } \n---\n` )
200
+ else writeFile ( args , args . infile , header + '\n' + ( content + oldContent ) . replace ( / \n + $ / , '\n' ) )
198
201
return resolve ( )
199
202
} )
200
203
} )
201
204
}
202
205
203
- function commit ( argv , newVersion ) {
206
+ function commit ( args , newVersion ) {
204
207
var msg = 'committing %s'
205
- var args = [ argv . infile ]
206
- var verify = argv . verify === false || argv . n ? '--no-verify ' : ''
208
+ var paths = [ args . infile ]
209
+ var verify = args . verify === false || args . n ? '--no-verify ' : ''
207
210
var toAdd = ''
208
211
// commit any of the config files that we've updated
209
212
// the version # for.
210
213
Object . keys ( configsToUpdate ) . forEach ( function ( p ) {
211
214
if ( configsToUpdate [ p ] ) {
212
215
msg += ' and %s'
213
- args . unshift ( path . basename ( p ) )
216
+ paths . unshift ( path . basename ( p ) )
214
217
toAdd += ' ' + path . relative ( process . cwd ( ) , p )
215
218
}
216
219
} )
217
- checkpoint ( argv , msg , args )
218
- return runExec ( argv , 'git add' + toAdd + ' ' + argv . infile )
220
+ checkpoint ( args , msg , paths )
221
+ return runExec ( args , 'git add' + toAdd + ' ' + args . infile )
219
222
. then ( ( ) => {
220
- return runExec ( argv , 'git commit ' + verify + ( argv . sign ? '-S ' : '' ) + ( argv . commitAll ? '' : ( argv . infile + toAdd ) ) + ' -m "' + formatCommitMessage ( argv . message , newVersion ) + '"' )
223
+ return runExec ( args , 'git commit ' + verify + ( args . sign ? '-S ' : '' ) + ( args . commitAll ? '' : ( args . infile + toAdd ) ) + ' -m "' + formatCommitMessage ( args . message , newVersion ) + '"' )
221
224
} )
222
225
}
223
226
224
227
function formatCommitMessage ( msg , newVersion ) {
225
228
return String ( msg ) . indexOf ( '%s' ) !== - 1 ? util . format ( msg , newVersion ) : msg
226
229
}
227
230
228
- function tag ( newVersion , pkgPrivate , argv ) {
231
+ function tag ( newVersion , pkgPrivate , args ) {
229
232
var tagOption
230
- if ( argv . sign ) {
233
+ if ( args . sign ) {
231
234
tagOption = '-s '
232
235
} else {
233
236
tagOption = '-a '
234
237
}
235
- checkpoint ( argv , 'tagging release %s' , [ newVersion ] )
236
- return runExec ( argv , 'git tag ' + tagOption + argv . tagPrefix + newVersion + ' -m "' + formatCommitMessage ( argv . message , newVersion ) + '"' )
238
+ checkpoint ( args , 'tagging release %s' , [ newVersion ] )
239
+ return runExec ( args , 'git tag ' + tagOption + args . tagPrefix + newVersion + ' -m "' + formatCommitMessage ( args . message , newVersion ) + '"' )
237
240
. then ( ( ) => {
238
241
var message = 'git push --follow-tags origin master'
239
242
if ( pkgPrivate !== true ) message += '; npm publish'
240
243
241
- checkpoint ( argv , 'Run `%s` to publish' , [ message ] , chalk . blue ( figures . info ) )
244
+ checkpoint ( args , 'Run `%s` to publish' , [ message ] , chalk . blue ( figures . info ) )
242
245
} )
243
246
}
244
247
245
- function createIfMissing ( argv ) {
248
+ function createIfMissing ( args ) {
246
249
try {
247
- accessSync ( argv . infile , fs . F_OK )
250
+ accessSync ( args . infile , fs . F_OK )
248
251
} catch ( err ) {
249
252
if ( err . code === 'ENOENT' ) {
250
- checkpoint ( argv , 'created %s' , [ argv . infile ] )
251
- argv . outputUnreleased = true
252
- fs . writeFileSync ( argv . infile , '\n' , 'utf-8 ')
253
+ checkpoint ( args , 'created %s' , [ args . infile ] )
254
+ args . outputUnreleased = true
255
+ writeFile ( args , args . infile , '\n' )
253
256
}
254
257
}
255
258
}
0 commit comments