From ab7291a60c08f64b003ceca23650931775721a52 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 8 Dec 2015 09:20:08 -0700 Subject: [PATCH] Rework callback error handling --- bin/gh-pages | 8 ++++---- lib/index.js | 41 ++++++++++++++++++++++------------------- package.json | 2 +- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/bin/gh-pages b/bin/gh-pages index 9b25895..81a5e32 100755 --- a/bin/gh-pages +++ b/bin/gh-pages @@ -25,12 +25,12 @@ ghpages.publish(path.join(process.cwd(), program.dist), { dotfiles: !!program.dotfiles, add: !!program.add, logger: function(message) { - console.log(message); + process.stderr.write(message + '\n'); } }, function(err) { - if (err != null) { - console.log(err) + if (err) { + process.stderr.write(err.message + '\n'); return process.exit(1); } - console.log('Published'); + process.stderr.write('Published\n'); }); diff --git a/lib/index.js b/lib/index.js index 19a0d5e..1658987 100644 --- a/lib/index.js +++ b/lib/index.js @@ -50,25 +50,12 @@ function getRepo(options) { * @param {Object} config Publish options. * @param {Function} callback Callback. */ - /*eslint-disable no-console */ exports.publish = function publish(basePath, config, callback) { if (typeof config === 'function') { callback = config; config = {}; } - var done = function(arguments) { - try { - (callback || function(err) { - if (err) { - console.log(err); - } - })(arguments); - } catch (err) { - console.error('errors during calling the callback: ', err); - } - } - var defaults = { add: false, git: 'git', @@ -94,6 +81,28 @@ exports.publish = function publish(basePath, config, callback) { options[c] = config[c]; } + function log(message) { + if (!options.silent) { + options.logger(message); + } + } + + if (!callback) { + callback = function(err) { + if (err) { + log(err.message); + } + }; + } + + function done(err) { + try { + callback(err); + } catch (err2) { + log('Publish callback threw: ', err2.message); + } + } + try { if (!fs.statSync(basePath).isDirectory()) { done(new Error('The "base" option must be an existing directory')); @@ -118,12 +127,6 @@ exports.publish = function publish(basePath, config, callback) { var only = glob.sync(options.only, {cwd: basePath}); - function log(message) { - if (!options.silent) { - options.logger(message); - } - } - git.exe(options.git); var repoUrl; diff --git a/package.json b/package.json index 0ee23cc..7032477 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ }, "main": "lib/index.js", "scripts": { - "pretest": "eslint lib test", + "pretest": "eslint lib test bin/gh-pages", "test": "mocha --recursive test" }, "dependencies": {