@@ -13,16 +13,16 @@ const checkpoint = require('./lib/checkpoint')
1313const printError = require ( './lib/print-error' )
1414const runExec = require ( './lib/run-exec' )
1515const runLifecycleScript = require ( './lib/run-lifecycle-script' )
16+ const writeFile = require ( './lib/write-file' )
1617
1718module . exports = function standardVersion ( argv ) {
1819 var pkgPath = path . resolve ( process . cwd ( ) , './package.json' )
1920 var pkg = require ( pkgPath )
2021 var newVersion = pkg . version
21- var scripts = argv . scripts || { }
2222 var defaults = require ( './defaults' )
2323 var args = Object . assign ( { } , defaults , argv )
2424
25- return runLifecycleScript ( args , 'prebump' , null , scripts )
25+ return runLifecycleScript ( args , 'prebump' , null )
2626 . then ( ( stdout ) => {
2727 if ( stdout && stdout . trim ( ) . length ) args . releaseAs = stdout . trim ( )
2828 return bumpVersion ( args . releaseAs )
@@ -36,13 +36,13 @@ module.exports = function standardVersion (argv) {
3636 checkpoint ( args , 'skip version bump on first release' , [ ] , chalk . red ( figures . cross ) )
3737 }
3838
39- return runLifecycleScript ( args , 'postbump' , newVersion , scripts )
39+ return runLifecycleScript ( args , 'postbump' , newVersion , args )
4040 } )
4141 . then ( ( ) => {
42- return outputChangelog ( args )
42+ return outputChangelog ( args , newVersion )
4343 } )
4444 . then ( ( ) => {
45- return runLifecycleScript ( args , 'precommit' , newVersion , scripts )
45+ return runLifecycleScript ( args , 'precommit' , newVersion , args )
4646 } )
4747 . then ( ( message ) => {
4848 if ( message && message . length ) args . message = message
@@ -61,7 +61,7 @@ module.exports = function standardVersion (argv) {
6161 * attempt to update the version # in a collection of common config
6262 * files, e.g., package.json, bower.json.
6363 *
64- * @param argv config object
64+ * @param args config object
6565 * @param newVersion version # to update to.
6666 * @return {string }
6767 */
@@ -78,7 +78,7 @@ function updateConfigs (args, newVersion) {
7878 var filename = path . basename ( configPath )
7979 checkpoint ( args , 'bumping version in ' + filename + ' from %s to %s' , [ config . version , newVersion ] )
8080 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' )
8282 // flag any config files that we modify the version # for
8383 // as having been updated.
8484 configsToUpdate [ configPath ] = true
@@ -171,19 +171,21 @@ function bumpVersion (releaseAs, callback) {
171171 } )
172172}
173173
174- function outputChangelog ( argv ) {
174+ function outputChangelog ( args , newVersion ) {
175175 return new Promise ( ( resolve , reject ) => {
176- createIfMissing ( argv )
176+ createIfMissing ( args )
177177 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' )
179179 // find the position of the last release and remove header:
180180 if ( oldContent . indexOf ( '<a name=' ) !== - 1 ) {
181181 oldContent = oldContent . substring ( oldContent . indexOf ( '<a name=' ) )
182182 }
183183 var content = ''
184+ var context
185+ if ( args . dryRun ) context = { version : newVersion }
184186 var changelogStream = conventionalChangelog ( {
185187 preset : 'angular'
186- } , undefined , { merges : null } )
188+ } , context , { merges : null } )
187189 . on ( 'error' , function ( err ) {
188190 return reject ( err )
189191 } )
@@ -193,63 +195,64 @@ function outputChangelog (argv) {
193195 } )
194196
195197 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' ) )
198201 return resolve ( )
199202 } )
200203 } )
201204}
202205
203- function commit ( argv , newVersion ) {
206+ function commit ( args , newVersion ) {
204207 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 ' : ''
207210 var toAdd = ''
208211 // commit any of the config files that we've updated
209212 // the version # for.
210213 Object . keys ( configsToUpdate ) . forEach ( function ( p ) {
211214 if ( configsToUpdate [ p ] ) {
212215 msg += ' and %s'
213- args . unshift ( path . basename ( p ) )
216+ paths . unshift ( path . basename ( p ) )
214217 toAdd += ' ' + path . relative ( process . cwd ( ) , p )
215218 }
216219 } )
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 )
219222 . 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 ) + '"' )
221224 } )
222225}
223226
224227function formatCommitMessage ( msg , newVersion ) {
225228 return String ( msg ) . indexOf ( '%s' ) !== - 1 ? util . format ( msg , newVersion ) : msg
226229}
227230
228- function tag ( newVersion , pkgPrivate , argv ) {
231+ function tag ( newVersion , pkgPrivate , args ) {
229232 var tagOption
230- if ( argv . sign ) {
233+ if ( args . sign ) {
231234 tagOption = '-s '
232235 } else {
233236 tagOption = '-a '
234237 }
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 ) + '"' )
237240 . then ( ( ) => {
238241 var message = 'git push --follow-tags origin master'
239242 if ( pkgPrivate !== true ) message += '; npm publish'
240243
241- checkpoint ( argv , 'Run `%s` to publish' , [ message ] , chalk . blue ( figures . info ) )
244+ checkpoint ( args , 'Run `%s` to publish' , [ message ] , chalk . blue ( figures . info ) )
242245 } )
243246}
244247
245- function createIfMissing ( argv ) {
248+ function createIfMissing ( args ) {
246249 try {
247- accessSync ( argv . infile , fs . F_OK )
250+ accessSync ( args . infile , fs . F_OK )
248251 } catch ( err ) {
249252 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' )
253256 }
254257 }
255258}
0 commit comments