mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
feature(tryRequire) options: add exit
This commit is contained in:
parent
3572866bd3
commit
6be2025a66
1 changed files with 19 additions and 5 deletions
|
|
@ -1,21 +1,23 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
var Util = require('../util');
|
||||
|
||||
module.exports = function tryRequire(name, options) {
|
||||
var module,
|
||||
o = options || {},
|
||||
|
||||
error = Util.exec.try(function() {
|
||||
error = tryCatch(function() {
|
||||
module = require(name);
|
||||
});
|
||||
|
||||
if (error)
|
||||
if (error) {
|
||||
if (o.log)
|
||||
Util.log(error.message);
|
||||
console.error(error.message);
|
||||
else if (o.callback)
|
||||
module = exec.bind(null, error);
|
||||
|
||||
if (o.exit)
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
return module;
|
||||
};
|
||||
|
|
@ -23,4 +25,16 @@
|
|||
function exec(error, callback) {
|
||||
callback(error);
|
||||
}
|
||||
|
||||
function tryCatch(fn) {
|
||||
var ret, error;
|
||||
|
||||
try {
|
||||
ret = fn();
|
||||
} catch(err) {
|
||||
error = err;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue