diff --git a/bin/semantic-release b/bin/semantic-release index 10de67f1..67fa9ccd 100755 --- a/bin/semantic-release +++ b/bin/semantic-release @@ -5,6 +5,8 @@ var abbrev = require('abbrev') var confirm = require('confirm-simple') var minimist = require('minimist') +var efh = require('../lib/error').standard + var argv = minimist(process.argv.slice(2), { alias: { d: 'debug', @@ -21,8 +23,6 @@ var npmArgv = process.env.npm_config_argv ? {_: []} if (~argv._.indexOf('pre')) { - var publish = false - // see src/restart.js if (npmArgv['semantic-release-rerun']) process.exit(0) // the `prepublish` hook is also executed when the package is installed @@ -30,47 +30,35 @@ if (~argv._.indexOf('pre')) { if (isAbbrev(npmArgv, 'install')) process.exit(0) return confirmCI(function () { - if (isAbbrev(npmArgv, 'publish')) publish = true - console.log('Determining new version') - require('../src/pre')(argv, function (err, result) { - if (err) { - console.log('Something went wrong.') - throw err - } + var publish = false + if (isAbbrev(npmArgv, 'publish')) publish = true + require('../src/pre')(argv, efh(function (result) { if (!result) { console.log('Nothing changed. Not publishing.') process.exit(1) } console.log('Publishing v' + result) - if (publish) require('../src/restart')(function (err) { - if (err) { - console.log('Something went wrong.') - throw err - } + if (!publish) process.exit(0) + require('../src/restart')(efh(function () { process.exit(1) - }) - }) + })) + })) }) } if (~argv._.indexOf('post')) { return confirmCI(function () { - require('../src/post')(argv, function (err) { - if (err) { - console.log('Something went wrong.') - throw err - } - + require('../src/post')(argv, efh(function () { // see src/restart.js if (npmArgv['semantic-release-rerun']) { console.log('Everything is alright :) npm will now print an error message that you can safely ignore.') } - }) + })) }) } diff --git a/lib/error.js b/lib/error.js new file mode 100644 index 00000000..25ffbe8c --- /dev/null +++ b/lib/error.js @@ -0,0 +1,14 @@ +'use strict' + +var efh = require('error-first-handler') + +module.exports = { + efh: efh, + standard: efh(function(err) { + console.log('Something went wrong:') + if (typeof err === 'string') return console.log(err) + if (err instanceof Error) return console.log(err.message) + if (err.message) return console.log(err.message) + console.log(err) + }) +} diff --git a/lib/type/commits.js b/lib/type/commits.js index 5cbe98e1..37aa5922 100644 --- a/lib/type/commits.js +++ b/lib/type/commits.js @@ -2,13 +2,12 @@ var git = require('conventional-changelog/lib/git') -module.exports = function (cb) { - git.latestTag(function (err, from) { - if (err) return cb(err) +var efh = require('../error').efh - git.getCommits({from: from}, function (err, commits) { - if (err) return cb(err) +module.exports = function (cb) { + git.latestTag(efh(cb)(function (from) { + git.getCommits({from: from}, efh(cb)(function (commits) { cb(null, commits) - }) - }) + })) + })) } diff --git a/lib/type/index.js b/lib/type/index.js index e17115ae..89f5f106 100644 --- a/lib/type/index.js +++ b/lib/type/index.js @@ -1,12 +1,11 @@ 'use strict' -var commits = require('./commits') var analyze = require('./analyze') +var commits = require('./commits') +var efh = require('../error').efh module.exports = function (cb) { - commits(function (err, commits) { - if (err) return cb(err) - + commits(efh(cb)(function (commits) { cb(null, analyze(commits)) - }) + })) } diff --git a/lib/version.js b/lib/version.js index bc0d0a14..c3863377 100644 --- a/lib/version.js +++ b/lib/version.js @@ -4,7 +4,7 @@ var exec = require('child_process').exec var unlink = require('fs').unlinkSync module.exports = function (pkg, cb) { - if (!pkg.name) return cb('Package must have a name') + if (!pkg.name) return cb(new Error('Package must have a name')) exec('npm show ' + pkg.name + ' version', function(err, stdout, stderr) { if (err) unlink('./npm-debug.log') diff --git a/package.json b/package.json index 6721360c..976ae468 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "abbrev": "^1.0.5", "confirm-simple": "^1.0.3", "conventional-changelog": "0.0.11", + "error-first-handler": "^1.0.1", "github": "^0.2.3", "github-url-from-git": "^1.4.0", "minimist": "^1.1.0", diff --git a/src/post.js b/src/post.js index ce99153e..47932365 100644 --- a/src/post.js +++ b/src/post.js @@ -8,6 +8,8 @@ var GitHubApi = require('github') var parseSlug = require('parse-github-repo-url') var parseUrl = require('github-url-from-git') +var efh = require('../lib/error').efh + var github = new GitHubApi({ version: '3.0.0' }) @@ -16,18 +18,14 @@ module.exports = function (options, cb) { var pkg = JSON.parse(readFile('./package.json')) var repository = pkg.repository ? pkg.repository.url : null - if (!repository) return cb('Package must have a repository') + if (!repository) return cb(new Error('Package must have a repository')) changelog({ version: pkg.version, repository: parseUrl(repository), file: false - }, function(err, log) { - if (err) return cb(err) - - exec('git rev-parse HEAD', function(err, hash) { - if (err) return cb(err) - + }, efh(cb)(function (log) { + exec('git rev-parse HEAD', efh(cb)(function (hash) { var ghRepo = parseSlug(repository) var release = { owner: ghRepo[0], @@ -43,10 +41,9 @@ module.exports = function (options, cb) { token: options.token }) - github.releases.createRelease(release, function(err) { - if (err) return cb(err) + github.releases.createRelease(release, efh(cb)(function () { cb(null, true) - }) - }) - }) + })) + })) + })) } diff --git a/src/pre.js b/src/pre.js index 4ecdf919..fe965f97 100644 --- a/src/pre.js +++ b/src/pre.js @@ -6,21 +6,19 @@ var semver = require('semver') var type = require('../lib/type') var version = require('../lib/version') +var efh = require('../lib/error').efh module.exports = function (options, cb) { - type(function (err, type) { - if (err) return cb(err) + type(efh(cb)(function (type) { if (!type) return cb(null, null) var path = './package.json' var pkg = JSON.parse(fs.readFileSync(path)) - version(pkg, function (err, version, unpublished) { - if (err) return cb(err) - + version(pkg, efh(cb)(function (version, unpublished) { pkg.version = unpublished ? '1.0.0' : semver.inc(version, type) if (!options.debug) fs.writeFileSync(path, JSON.stringify(pkg, null, 2)) cb(null, pkg.version) - }) - }) + })) + })) }