mirror of
https://github.com/scratchfoundation/gh-pages.git
synced 2024-11-24 16:28:02 -05:00
Rework callback error handling
This commit is contained in:
parent
0794c0b142
commit
ab7291a60c
3 changed files with 27 additions and 24 deletions
|
@ -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');
|
||||
});
|
||||
|
|
41
lib/index.js
41
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;
|
||||
|
|
|
@ -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": {
|
||||
|
|
Loading…
Reference in a new issue