Permalink
Browse files

another refactor

  • Loading branch information...
1 parent 219358c commit ff0c0283e07272996bdddb046a9bc40585046768 @chjj committed Apr 11, 2012
Showing with 34 additions and 18 deletions.
  1. +34 −18 lib/daemonic.js
View
@@ -17,14 +17,14 @@ function daemonic() {
*/
daemonic.daemonize = function(opt, func) {
- if (badOptions(opt)) return;
+ if (!checkOptions(opt)) return exports;
var opt = opt || {}
, argv = process.argv.slice()
, ps
, i;
- if (exports.isDaemon) return;
+ if (exports.isDaemon) return exports;
i = process.argv.indexOf(exports.arg);
if (~i) {
@@ -50,8 +50,15 @@ daemonic.daemonize = function(opt, func) {
*/
daemonic.fork = function(name, argv, opt, func) {
+ if (arguments.length === 2) {
+ func = arguments[1];
+ opt = arguments[0];
+ argv = opt.argv.slice(1);
+ name = opt.argv[0];
+ }
+
var opt = opt || {}
- , argv = [name].concat(argv)
+ , argv = [name].concat(argv || [])
, func = func || function() {}
, ps
, i;
@@ -69,6 +76,15 @@ daemonic.fork = function(name, argv, opt, func) {
if (!isFinite(pid)) pid = -1;
func(null, pid);
});
+
+ return {
+ __proto__: require('events').EventEmitter.prototype,
+ get pid() { return pid; }
+ // Something like:
+ // stdin: createSocket(createPipe(), false),
+ // stdout: createSocket(createPipe(), true),
+ // stderr: createSocket(createPipe(), true)
+ };
};
/**
@@ -158,26 +174,26 @@ function env(env) {
return 'env - ' + out.join(' ');
}
-function badOptions(opt) {
+function checkOptions(opt) {
var options = opt && opt.options;
+ if (!options || !options.length) {
+ return true;
+ }
+
+ var found
+ , l = options.length
+ , i = 0
+ , j;
- if (options && options.length) {
- var found = false
- , argv = process.argv
- , l = options.length
- , i = 0
- , j;
-
- for (; i < l; i++) {
- j = argv.indexOf(options[i]);
- if (~j) {
- argv.splice(j, 1);
- found = true;
- }
+ for (; i < l; i++) {
+ j = process.argv.indexOf(options[i]);
+ if (j !== -1) {
+ process.argv.splice(j, 1);
+ found = true;
}
}
- return !found;
+ return found;
}
/**

0 comments on commit ff0c028

Please sign in to comment.