Skip to content

Commit

Permalink
chore: switch to using promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Jun 4, 2017
1 parent d32a739 commit e37092a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 127 deletions.
7 changes: 3 additions & 4 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var cmdParser = require('../command')
if (process.version.match(/v(\d+)\./)[1] < 4) {
console.error('standard-version: Node v4 or greater is required. `standard-version` did not run.')
} else {
standardVersion(cmdParser.argv, function (err) {
if (err) {
standardVersion(cmdParser.argv)
.catch(() => {
process.exit(1)
}
})
})
}
150 changes: 67 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,42 @@ const printError = require('./lib/print-error')
const runExec = require('./lib/run-exec')
const runLifecycleHook = require('./lib/run-lifecycle-hook')

module.exports = function standardVersion (argv, done) {
module.exports = function standardVersion (argv) {
var pkgPath = path.resolve(process.cwd(), './package.json')
var pkg = require(pkgPath)
var newVersion = pkg.version
var hooks = argv.hooks || {}
var defaults = require('./defaults')
var args = Object.assign({}, defaults, argv)

return bumpVersion(args.releaseAs, function (err, release) {
if (err) {
printError(args, err.message)
return done(err)
}

var newVersion = pkg.version

if (!args.firstRelease) {
var releaseType = getReleaseType(args.prerelease, release.releaseType, pkg.version)
newVersion = semver.valid(releaseType) || semver.inc(pkg.version, releaseType, args.prerelease)
updateConfigs(args, newVersion)
} else {
checkpoint(args, 'skip version bump on first release', [], chalk.red(figures.cross))
}
runLifecycleHook(args, 'postbump', newVersion, hooks, function (err) {
if (err) {
printError(args, err.message)
return done(err)
return bumpVersion(args.releaseAs)
.then((release) => {
if (!args.firstRelease) {
var releaseType = getReleaseType(args.prerelease, release.releaseType, pkg.version)
newVersion = semver.valid(releaseType) || semver.inc(pkg.version, releaseType, args.prerelease)
updateConfigs(args, newVersion)
} else {
checkpoint(args, 'skip version bump on first release', [], chalk.red(figures.cross))
}

outputChangelog(args, function (err) {
if (err) {
return done(err)
}
runLifecycleHook(args, 'precommit', newVersion, hooks, function (err) {
if (err) {
printError(args, err.message)
return done(err)
}
commit(args, newVersion, function (err) {
if (err) {
return done(err)
}
return tag(newVersion, pkg.private, args, done)
})
})
})
return runLifecycleHook(args, 'postbump', newVersion, hooks)
})
.then(() => {
return outputChangelog(args)
})
.then(() => {
return runLifecycleHook(args, 'precommit', newVersion, hooks)
})
.then(() => {
return commit(args, newVersion)
})
.then(() => {
return tag(newVersion, pkg.private, args)
})
.catch((err) => {
printError(args, err.message)
throw err
})
})
}

/**
Expand Down Expand Up @@ -160,47 +149,52 @@ function getTypePriority (type) {
}

function bumpVersion (releaseAs, callback) {
if (releaseAs) {
callback(null, {
releaseType: releaseAs
})
} else {
conventionalRecommendedBump({
preset: 'angular'
}, function (err, release) {
callback(err, release)
})
}
return new Promise((resolve, reject) => {
if (releaseAs) {
return resolve({
releaseType: releaseAs
})
} else {
conventionalRecommendedBump({
preset: 'angular'
}, function (err, release) {
if (err) return reject(err)
else return resolve(release)
})
}
})
}

function outputChangelog (argv, cb) {
createIfMissing(argv)
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 = fs.readFileSync(argv.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 changelogStream = conventionalChangelog({
preset: 'angular'
}, undefined, {merges: null})
.on('error', function (err) {
return cb(err)
})
function outputChangelog (argv) {
return new Promise((resolve, reject) => {
createIfMissing(argv)
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 = fs.readFileSync(argv.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 changelogStream = conventionalChangelog({
preset: 'angular'
}, undefined, {merges: null})
.on('error', function (err) {
return reject(err)
})

changelogStream.on('data', function (buffer) {
content += buffer.toString()
})
changelogStream.on('data', function (buffer) {
content += buffer.toString()
})

changelogStream.on('end', function () {
checkpoint(argv, 'outputting changes to %s', [argv.infile])
fs.writeFileSync(argv.infile, header + '\n' + (content + oldContent).replace(/\n+$/, '\n'), 'utf-8')
return cb()
changelogStream.on('end', function () {
checkpoint(argv, 'outputting changes to %s', [argv.infile])
fs.writeFileSync(argv.infile, header + '\n' + (content + oldContent).replace(/\n+$/, '\n'), 'utf-8')
return resolve()
})
})
}

function commit (argv, newVersion, cb) {
function commit (argv, newVersion) {
var msg = 'committing %s'
var args = [argv.infile]
var verify = argv.verify === false || argv.n ? '--no-verify ' : ''
Expand All @@ -219,19 +213,13 @@ function commit (argv, newVersion, cb) {
.then(() => {
return runExec(argv, 'git commit ' + verify + (argv.sign ? '-S ' : '') + (argv.commitAll ? '' : (argv.infile + toAdd)) + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"')
})
.then(() => {
return cb()
})
.catch((err) => {
return cb(err)
})
}

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

function tag (newVersion, pkgPrivate, argv, cb) {
function tag (newVersion, pkgPrivate, argv) {
var tagOption
if (argv.sign) {
tagOption = '-s '
Expand All @@ -245,10 +233,6 @@ function tag (newVersion, pkgPrivate, argv, cb) {
if (pkgPrivate !== true) message += '; npm publish'

checkpoint(argv, 'Run `%s` to publish', [message], chalk.blue(figures.info))
return cb()
})
.catch((err) => {
return cb(err)
})
}

Expand Down
13 changes: 2 additions & 11 deletions lib/run-lifecycle-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@ const figures = require('figures')
const runExec = require('./run-exec')

module.exports = function (argv, hookName, newVersion, hooks, cb) {
if (!hooks[hookName]) {
cb()
return
}
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))
runExec(argv, command)
.then(() => {
return cb()
})
.catch((err) => {
return cb(err)
})
return runExec(argv, command)
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"test": "nyc mocha --timeout=20000 test.js",
"release": "bin/cli.js"
},
"nyc": {
"exclude": ["tmp/**"]
},
"repository": {
"type": "git",
"url": "git+https://github.com/conventional-changelog/standard-version.git"
Expand Down Expand Up @@ -42,7 +45,6 @@
"yargs": "^8.0.1"
},
"devDependencies": {
"bluebird": "^3.4.6",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"mocha": "^3.1.0",
Expand Down
54 changes: 26 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ var stream = require('stream')
var mockGit = require('mock-git')
var mockery = require('mockery')
var semver = require('semver')
var Promise = require('bluebird')
var cli = require('./command')
var standardVersion = require('./index')

var should = require('chai').should()
require('chai').should()

var cliPath = path.resolve(__dirname, './bin/cli.js')

Expand All @@ -38,7 +37,7 @@ function execCli (argString) {
}

function execCliAsync (argString) {
return Promise.promisify(standardVersion)(cli.parse('standard-version ' + argString + ' --silent'))
return standardVersion(cli.parse('standard-version ' + argString + ' --silent'))
}

function writePackageJson (version, option) {
Expand Down Expand Up @@ -544,11 +543,11 @@ describe('standard-version', function () {
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')

require('./index')({silent: true}, function (err) {
should.exist(err)
err.message.should.match(/bump err/)
done()
})
require('./index')({silent: true})
.catch((err) => {
err.message.should.match(/bump err/)
done()
})
})
})

Expand All @@ -574,11 +573,11 @@ describe('standard-version', function () {
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')

require('./index')({silent: true}, function (err) {
should.exist(err)
err.message.should.match(/changelog err/)
done()
})
require('./index')({silent: true})
.catch((err) => {
err.message.should.match(/changelog err/)
return done()
})
})
})

Expand All @@ -587,15 +586,14 @@ describe('standard-version', function () {
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')

require('./index')({silent: true}, function (err) {
should.not.exist(err)

// check last commit message
shell.exec('git log --oneline -n1').stdout.should.match(/chore\(release\): 1\.1\.0/)
// check annotated tag message
shell.exec('git tag -l -n1 v1.1.0').stdout.should.match(/chore\(release\): 1\.1\.0/)
done()
})
require('./index')({silent: true})
.then(() => {
// check last commit message
shell.exec('git log --oneline -n1').stdout.should.match(/chore\(release\): 1\.1\.0/)
// check annotated tag message
shell.exec('git tag -l -n1 v1.1.0').stdout.should.match(/chore\(release\): 1\.1\.0/)
done()
})
})

describe('bower.json support', function () {
Expand All @@ -607,12 +605,12 @@ describe('standard-version', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
require('./index')({silent: true}, function (err) {
if (err) return done(err)
JSON.parse(fs.readFileSync('package.json', 'utf-8')).version.should.equal('1.1.0')
getPackageVersion().should.equal('1.1.0')
done()
})
require('./index')({silent: true})
.then(() => {
JSON.parse(fs.readFileSync('package.json', 'utf-8')).version.should.equal('1.1.0')
getPackageVersion().should.equal('1.1.0')
return done()
})
})
})
})

0 comments on commit e37092a

Please sign in to comment.