refator: use error-first-handler

This commit is contained in:
Stephan Bönnemann 2015-02-03 18:54:17 +01:00
parent 19fd15c1dc
commit 019aeeabe1
8 changed files with 51 additions and 55 deletions

View File

@ -5,6 +5,8 @@ var abbrev = require('abbrev')
var confirm = require('confirm-simple') var confirm = require('confirm-simple')
var minimist = require('minimist') var minimist = require('minimist')
var efh = require('../lib/error').standard
var argv = minimist(process.argv.slice(2), { var argv = minimist(process.argv.slice(2), {
alias: { alias: {
d: 'debug', d: 'debug',
@ -21,8 +23,6 @@ var npmArgv = process.env.npm_config_argv ?
{_: []} {_: []}
if (~argv._.indexOf('pre')) { if (~argv._.indexOf('pre')) {
var publish = false
// see src/restart.js // see src/restart.js
if (npmArgv['semantic-release-rerun']) process.exit(0) if (npmArgv['semantic-release-rerun']) process.exit(0)
// the `prepublish` hook is also executed when the package is installed // 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) if (isAbbrev(npmArgv, 'install')) process.exit(0)
return confirmCI(function () { return confirmCI(function () {
if (isAbbrev(npmArgv, 'publish')) publish = true
console.log('Determining new version') console.log('Determining new version')
require('../src/pre')(argv, function (err, result) { var publish = false
if (err) { if (isAbbrev(npmArgv, 'publish')) publish = true
console.log('Something went wrong.')
throw err
}
require('../src/pre')(argv, efh(function (result) {
if (!result) { if (!result) {
console.log('Nothing changed. Not publishing.') console.log('Nothing changed. Not publishing.')
process.exit(1) process.exit(1)
} }
console.log('Publishing v' + result) console.log('Publishing v' + result)
if (publish) require('../src/restart')(function (err) { if (!publish) process.exit(0)
if (err) {
console.log('Something went wrong.')
throw err
}
require('../src/restart')(efh(function () {
process.exit(1) process.exit(1)
}) }))
}) }))
}) })
} }
if (~argv._.indexOf('post')) { if (~argv._.indexOf('post')) {
return confirmCI(function () { return confirmCI(function () {
require('../src/post')(argv, function (err) { require('../src/post')(argv, efh(function () {
if (err) {
console.log('Something went wrong.')
throw err
}
// see src/restart.js // see src/restart.js
if (npmArgv['semantic-release-rerun']) { if (npmArgv['semantic-release-rerun']) {
console.log('Everything is alright :) npm will now print an error message that you can safely ignore.') console.log('Everything is alright :) npm will now print an error message that you can safely ignore.')
} }
}) }))
}) })
} }

14
lib/error.js Normal file
View File

@ -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)
})
}

View File

@ -2,13 +2,12 @@
var git = require('conventional-changelog/lib/git') var git = require('conventional-changelog/lib/git')
module.exports = function (cb) { var efh = require('../error').efh
git.latestTag(function (err, from) {
if (err) return cb(err)
git.getCommits({from: from}, function (err, commits) { module.exports = function (cb) {
if (err) return cb(err) git.latestTag(efh(cb)(function (from) {
git.getCommits({from: from}, efh(cb)(function (commits) {
cb(null, commits) cb(null, commits)
}) }))
}) }))
} }

View File

@ -1,12 +1,11 @@
'use strict' 'use strict'
var commits = require('./commits')
var analyze = require('./analyze') var analyze = require('./analyze')
var commits = require('./commits')
var efh = require('../error').efh
module.exports = function (cb) { module.exports = function (cb) {
commits(function (err, commits) { commits(efh(cb)(function (commits) {
if (err) return cb(err)
cb(null, analyze(commits)) cb(null, analyze(commits))
}) }))
} }

View File

@ -4,7 +4,7 @@ var exec = require('child_process').exec
var unlink = require('fs').unlinkSync var unlink = require('fs').unlinkSync
module.exports = function (pkg, cb) { 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) { exec('npm show ' + pkg.name + ' version', function(err, stdout, stderr) {
if (err) unlink('./npm-debug.log') if (err) unlink('./npm-debug.log')

View File

@ -11,6 +11,7 @@
"abbrev": "^1.0.5", "abbrev": "^1.0.5",
"confirm-simple": "^1.0.3", "confirm-simple": "^1.0.3",
"conventional-changelog": "0.0.11", "conventional-changelog": "0.0.11",
"error-first-handler": "^1.0.1",
"github": "^0.2.3", "github": "^0.2.3",
"github-url-from-git": "^1.4.0", "github-url-from-git": "^1.4.0",
"minimist": "^1.1.0", "minimist": "^1.1.0",

View File

@ -8,6 +8,8 @@ var GitHubApi = require('github')
var parseSlug = require('parse-github-repo-url') var parseSlug = require('parse-github-repo-url')
var parseUrl = require('github-url-from-git') var parseUrl = require('github-url-from-git')
var efh = require('../lib/error').efh
var github = new GitHubApi({ var github = new GitHubApi({
version: '3.0.0' version: '3.0.0'
}) })
@ -16,18 +18,14 @@ module.exports = function (options, cb) {
var pkg = JSON.parse(readFile('./package.json')) var pkg = JSON.parse(readFile('./package.json'))
var repository = pkg.repository ? pkg.repository.url : null 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({ changelog({
version: pkg.version, version: pkg.version,
repository: parseUrl(repository), repository: parseUrl(repository),
file: false file: false
}, function(err, log) { }, efh(cb)(function (log) {
if (err) return cb(err) exec('git rev-parse HEAD', efh(cb)(function (hash) {
exec('git rev-parse HEAD', function(err, hash) {
if (err) return cb(err)
var ghRepo = parseSlug(repository) var ghRepo = parseSlug(repository)
var release = { var release = {
owner: ghRepo[0], owner: ghRepo[0],
@ -43,10 +41,9 @@ module.exports = function (options, cb) {
token: options.token token: options.token
}) })
github.releases.createRelease(release, function(err) { github.releases.createRelease(release, efh(cb)(function () {
if (err) return cb(err)
cb(null, true) cb(null, true)
}) }))
}) }))
}) }))
} }

View File

@ -6,21 +6,19 @@ var semver = require('semver')
var type = require('../lib/type') var type = require('../lib/type')
var version = require('../lib/version') var version = require('../lib/version')
var efh = require('../lib/error').efh
module.exports = function (options, cb) { module.exports = function (options, cb) {
type(function (err, type) { type(efh(cb)(function (type) {
if (err) return cb(err)
if (!type) return cb(null, null) if (!type) return cb(null, null)
var path = './package.json' var path = './package.json'
var pkg = JSON.parse(fs.readFileSync(path)) var pkg = JSON.parse(fs.readFileSync(path))
version(pkg, function (err, version, unpublished) { version(pkg, efh(cb)(function (version, unpublished) {
if (err) return cb(err)
pkg.version = unpublished ? '1.0.0' : semver.inc(version, type) pkg.version = unpublished ? '1.0.0' : semver.inc(version, type)
if (!options.debug) fs.writeFileSync(path, JSON.stringify(pkg, null, 2)) if (!options.debug) fs.writeFileSync(path, JSON.stringify(pkg, null, 2))
cb(null, pkg.version) cb(null, pkg.version)
}) }))
}) }))
} }