Skip to content

Commit

Permalink
chore: switch to consistently using args rather than argv
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Jun 5, 2017
1 parent 42e77c6 commit 3f23072
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
52 changes: 26 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function standardVersion (argv) {
var defaults = require('./defaults')
var args = Object.assign({}, defaults, argv)

return runLifecycleScript(args, 'prebump', null, args)
return runLifecycleScript(args, 'prebump', null)
.then((stdout) => {
if (stdout && stdout.trim().length) args.releaseAs = stdout.trim()
return bumpVersion(args.releaseAs)
Expand Down Expand Up @@ -61,7 +61,7 @@ module.exports = function standardVersion (argv) {
* attempt to update the version # in a collection of common config
* files, e.g., package.json, bower.json.
*
* @param argv config object
* @param args config object
* @param newVersion version # to update to.
* @return {string}
*/
Expand Down Expand Up @@ -171,18 +171,18 @@ function bumpVersion (releaseAs, callback) {
})
}

function outputChangelog (argv, newVersion) {
function outputChangelog (args, newVersion) {
return new Promise((resolve, reject) => {
createIfMissing(argv)
createIfMissing(args)
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'
var oldContent = argv.dryRun ? '' : fs.readFileSync(argv.infile, 'utf-8')
var oldContent = args.dryRun ? '' : fs.readFileSync(args.infile, 'utf-8')
// find the position of the last release and remove header:
if (oldContent.indexOf('<a name=') !== -1) {
oldContent = oldContent.substring(oldContent.indexOf('<a name='))
}
var content = ''
var context
if (argv.dryRun) context = {version: newVersion}
if (args.dryRun) context = {version: newVersion}
var changelogStream = conventionalChangelog({
preset: 'angular'
}, context, {merges: null})
Expand All @@ -195,64 +195,64 @@ function outputChangelog (argv, newVersion) {
})

changelogStream.on('end', function () {
checkpoint(argv, 'outputting changes to %s', [argv.infile])
if (argv.dryRun) console.log(`\n---\n${chalk.gray(content.trim())}\n---\n`)
else writeFile(argv, argv.infile, header + '\n' + (content + oldContent).replace(/\n+$/, '\n'))
checkpoint(args, 'outputting changes to %s', [args.infile])
if (args.dryRun) console.info(`\n---\n${chalk.gray(content.trim())}\n---\n`)
else writeFile(args, args.infile, header + '\n' + (content + oldContent).replace(/\n+$/, '\n'))
return resolve()
})
})
}

function commit (argv, newVersion) {
function commit (args, newVersion) {
var msg = 'committing %s'
var args = [argv.infile]
var verify = argv.verify === false || argv.n ? '--no-verify ' : ''
var paths = [args.infile]
var verify = args.verify === false || args.n ? '--no-verify ' : ''
var toAdd = ''
// commit any of the config files that we've updated
// the version # for.
Object.keys(configsToUpdate).forEach(function (p) {
if (configsToUpdate[p]) {
msg += ' and %s'
args.unshift(path.basename(p))
paths.unshift(path.basename(p))
toAdd += ' ' + path.relative(process.cwd(), p)
}
})
checkpoint(argv, msg, args)
return runExec(argv, 'git add' + toAdd + ' ' + argv.infile)
checkpoint(args, msg, paths)
return runExec(args, 'git add' + toAdd + ' ' + args.infile)
.then(() => {
return runExec(argv, 'git commit ' + verify + (argv.sign ? '-S ' : '') + (argv.commitAll ? '' : (argv.infile + toAdd)) + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"')
return runExec(args, 'git commit ' + verify + (args.sign ? '-S ' : '') + (args.commitAll ? '' : (args.infile + toAdd)) + ' -m "' + formatCommitMessage(args.message, newVersion) + '"')
})
}

function formatCommitMessage (msg, newVersion) {
return String(msg).indexOf('%s') !== -1 ? util.format(msg, newVersion) : msg
}

function tag (newVersion, pkgPrivate, argv) {
function tag (newVersion, pkgPrivate, args) {
var tagOption
if (argv.sign) {
if (args.sign) {
tagOption = '-s '
} else {
tagOption = '-a '
}
checkpoint(argv, 'tagging release %s', [newVersion])
return runExec(argv, 'git tag ' + tagOption + argv.tagPrefix + newVersion + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"')
checkpoint(args, 'tagging release %s', [newVersion])
return runExec(args, 'git tag ' + tagOption + args.tagPrefix + newVersion + ' -m "' + formatCommitMessage(args.message, newVersion) + '"')
.then(() => {
var message = 'git push --follow-tags origin master'
if (pkgPrivate !== true) message += '; npm publish'

checkpoint(argv, 'Run `%s` to publish', [message], chalk.blue(figures.info))
checkpoint(args, 'Run `%s` to publish', [message], chalk.blue(figures.info))
})
}

function createIfMissing (argv) {
function createIfMissing (args) {
try {
accessSync(argv.infile, fs.F_OK)
accessSync(args.infile, fs.F_OK)
} catch (err) {
if (err.code === 'ENOENT') {
checkpoint(argv, 'created %s', [argv.infile])
argv.outputUnreleased = true
writeFile(argv, argv.infile, '\n')
checkpoint(args, 'created %s', [args.infile])
args.outputUnreleased = true
writeFile(args, args.infile, '\n')
}
}
}
6 changes: 3 additions & 3 deletions lib/checkpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const chalk = require('chalk')
const figures = require('figures')
const util = require('util')

module.exports = function (argv, msg, args, figure) {
module.exports = function (args, msg, vars, figure) {
const defaultFigure = args.dryRun ? chalk.yellow(figures.tick) : chalk.green(figures.tick)
if (!argv.silent) {
console.info((figure || defaultFigure) + ' ' + util.format.apply(util, [msg].concat(args.map(function (arg) {
if (!args.silent) {
console.info((figure || defaultFigure) + ' ' + util.format.apply(util, [msg].concat(vars.map(function (arg) {
return chalk.bold(arg)
}))))
}
Expand Down
4 changes: 2 additions & 2 deletions lib/print-error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const chalk = require('chalk')

module.exports = function (argv, msg, opts) {
if (!argv.silent) {
module.exports = function (args, msg, opts) {
if (!args.silent) {
opts = Object.assign({
level: 'error',
color: 'red'
Expand Down
8 changes: 4 additions & 4 deletions lib/run-exec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const exec = require('child_process').exec
const printError = require('./print-error')

module.exports = function (argv, cmd) {
if (argv.dryRun) return Promise.resolve()
module.exports = function (args, cmd) {
if (args.dryRun) return Promise.resolve()
return new Promise((resolve, reject) => {
// Exec given cmd and handle possible errors
exec(cmd, function (err, stdout, stderr) {
// If exec returns content in stderr, but no error, print it as a warning
// If exec returns an error, print it and exit with return code 1
if (err) {
printError(argv, stderr || err.message)
printError(args, stderr || err.message)
return reject(err)
} else if (stderr) {
printError(argv, stderr, {level: 'warn', color: 'yellow'})
printError(args, stderr, {level: 'warn', color: 'yellow'})
}
return resolve(stdout)
})
Expand Down
8 changes: 4 additions & 4 deletions lib/run-lifecycle-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const checkpoint = require('./checkpoint')
const figures = require('figures')
const runExec = require('./run-exec')

module.exports = function (argv, hookName, newVersion, hooks, cb) {
module.exports = function (args, hookName, newVersion, hooks, cb) {
if (!hooks[hookName]) return Promise.resolve()
var command = hooks[hookName] + ' --new-version="' + newVersion + '"'
checkpoint(argv, 'Running lifecycle hook "%s"', [hookName])
checkpoint(argv, '- hook command: "%s"', [command], chalk.blue(figures.info))
return runExec(argv, command)
checkpoint(args, 'Running lifecycle hook "%s"', [hookName])
checkpoint(args, '- hook command: "%s"', [command], chalk.blue(figures.info))
return runExec(args, command)
}
8 changes: 4 additions & 4 deletions lib/run-lifecycle-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const checkpoint = require('./checkpoint')
const figures = require('figures')
const runExec = require('./run-exec')

module.exports = function (argv, hookName, newVersion, args) {
module.exports = function (args, hookName, newVersion) {
const scripts = args.scripts
if (!scripts || !scripts[hookName]) return Promise.resolve()
var command = scripts[hookName]
if (newVersion) command += ' --new-version="' + newVersion + '"'
checkpoint(argv, 'Running lifecycle script "%s"', [hookName])
checkpoint(argv, '- execute command: "%s"', [command], chalk.blue(figures.info))
return runExec(argv, command)
checkpoint(args, 'Running lifecycle script "%s"', [hookName])
checkpoint(args, '- execute command: "%s"', [command], chalk.blue(figures.info))
return runExec(args, command)
}
4 changes: 2 additions & 2 deletions lib/write-file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')

module.exports = function (argv, filePath, content) {
if (argv.dryRun) return
module.exports = function (args, filePath, content) {
if (args.dryRun) return
fs.writeFileSync(filePath, content, 'utf8')
}

0 comments on commit 3f23072

Please sign in to comment.