Permalink
Browse files

Add exit_cb function for process exit handling with custom success an…

…d error callbacks.
  • Loading branch information...
1 parent 9734c7a commit f18812c632b3727a661a7e5bcf2b88acd954b692 @cliffano committed May 2, 2012
Showing with 72 additions and 2 deletions.
  1. +36 −1 lib/cli.js
  2. +36 −1 test/cli.js
View
@@ -22,4 +22,39 @@ function exit(err, result) {
}
}
-exports.exit = exit;
+/**
+ * cli#exit_cb([errorCb], [successCb])
+ * - errorCb(Function): error callback accepts error argument, defaults to logging to console error
+ * - successCb(Function): success callback accepts result argument, defaults to logging to console log
+ *
+ * A higher order function that returns a process exit callback,
+ * with error and success callbacks to handle error and result accordingly.
+ * Exit status code 1 indicates an error, exit status code 0 indicates a success.
+ **/
+function exit_cb(errorCb, successCb) {
+
+ if (!errorCb) {
+ errorCb = function (err) {
+ console.error(err.message);
+ };
+ }
+
+ if (!successCb) {
+ successCb = function (result) {
+ console.log(result);
+ };
+ }
+
+ return function (err, result) {
+ if (err) {
+ errorCb(err);
+ process.exit(1);
+ } else {
+ successCb(result);
+ process.exit(0);
+ }
+ };
+}
+
+exports.exit = exit;
+exports.exit_cb = exit_cb;
View
@@ -29,11 +29,46 @@ describe('cli', function () {
assert.equal(checks.process_exit_code, 0);
});
- it('should exit with status code 1 when error exists and logs the error message', function () {
+ it('should exit with status code 1 and logs the error message when error exists', function () {
cli.exit(new Error('some error'));
assert.equal(checks.console_error_messages.length, 1);
assert.equal(checks.console_error_messages[0], 'some error');
assert.equal(checks.process_exit_code, 1);
});
});
+
+ describe('exit_cb', function () {
+
+ it('should exit with status code 0 and logs the result when error does not exist and no success callback is specified', function () {
+ cli.exit_cb()(null, 'some success');
+ assert.equal(checks.console_log_messages.length, 1);
+ assert.equal(checks.console_log_messages[0], 'some success');
+ assert.equal(checks.process_exit_code, 0);
+ });
+
+ it('should exit with status code 1 and logs the error message when error exists and no error callback is specified', function () {
+ cli.exit_cb()(new Error('some error'));
+ assert.equal(checks.console_error_messages.length, 1);
+ assert.equal(checks.console_error_messages[0], 'some error');
+ assert.equal(checks.process_exit_code, 1);
+ });
+
+ it('should exit with status code 0 and call success callback when error does not exist and success callback is specified', function () {
+ function successCb(result) {
+ checks.success_result = result;
+ }
+ cli.exit_cb(null, successCb)(null, 'some success');
+ assert.equal(checks.success_result, 'some success');
+ assert.equal(checks.process_exit_code, 0);
+ });
+
+ it('should exit with status code 1 and call error callback when error exists and error callback is specified', function () {
+ function errorCb(err) {
+ checks.error_err = err;
+ }
+ cli.exit_cb(errorCb)(new Error('some error'));
+ assert.equal(checks.error_err.message, 'some error');
+ assert.equal(checks.process_exit_code, 1);
+ });
+ });
});

0 comments on commit f18812c

Please sign in to comment.