Skip to content

Commit

Permalink
Merge pull request #86 from jugglinmike/remove-daemon
Browse files Browse the repository at this point in the history
Remove daemon support
  • Loading branch information
chriso committed Aug 16, 2016
2 parents 9f4c00b + fd6bc4d commit ed90515
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 212 deletions.
50 changes: 0 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,6 @@ cli.withStdinLines(function(lines, newline) {
});
```

### static.js

Let's create a static file server with daemon support to see the opts parser + plugins in use - note: this requires `npm install creationix daemon`

```javascript
var cli = require('cli').enable('daemon', 'status'); //Enable 2 plugins

cli.parse({
log: ['l', 'Enable logging'],
port: ['p', 'Listen on this port', 'number', 8080],
serve: [false, 'Serve static files from PATH', 'path', './public']
});

cli.main(function(args, options) {
var server, middleware = [];

if (options.log) {
this.debug('Enabling logging');
middleware.push(require('creationix/log')());
}

this.debug('Serving files from ' + options.serve);
middleware.push(require('creationix/static')('/', options.serve, 'index.html'));

server = this.createServer(middleware).listen(options.port);

this.ok('Listening on port ' + options.port);
});
```

To output usage information

```bash
$ ./static.js --help
```

To create a daemon that serves files from */tmp*, run

```bash
$ ./static.js -ld --serve=/tmp
```

For more examples, see [./examples](https://github.com/chriso/cli/tree/master/examples)

## Command Line Arguments Parser

cli takes an object as a map for the arguments you wish to parse.
Expand Down Expand Up @@ -209,12 +165,6 @@ cli.ok(msg);

Enables glob matching of arguments

**daemon** - *requires* `npm install daemon`

Adds `-d,--daemon ARG` for daemonizing the process and controlling the resulting daemon

`ARG` can be either start (default), stop, restart, pid (outputs the daemon's pid if it's running), or log (output the daemon's stdout+stderr)

**timeout**

Adds `-t,--timeout N` to exit the process after N seconds with an error
Expand Down
112 changes: 3 additions & 109 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var cli = exports,
argv, curr_opt, curr_val, full_opt, is_long,
short_tags = [], opt_list, parsed = {},
usage, argv_parsed, command_list, commands,
daemon, daemon_arg, show_debug;
show_debug;

cli.app = null;
cli.version = null;
Expand Down Expand Up @@ -85,7 +85,6 @@ if (process.env.NODE_DISABLE_COLORS || process.env.TERM === 'dumb') {
var enable = {
help: true, //Adds -h, --help
version: false, //Adds -v,--version => gets version by parsing a nearby package.json
daemon: false, //Adds -d,--daemon [ARG] => (see cli.daemon() below)
status: false, //Adds -k,--no-color & --debug => display plain status messages /display debug messages
timeout: false, //Adds -t,--timeout N => timeout the process after N seconds
catchall: false, //Adds -c,--catch => catch and output uncaughtExceptions
Expand All @@ -94,16 +93,6 @@ var enable = {
cli.enable = function (/*plugins*/) {
Array.prototype.slice.call(arguments).forEach(function (plugin) {
switch (plugin) {
case 'daemon':
try {
daemon = require('daemon');
if (typeof daemon.daemonize !== 'function') {
throw 'Invalid module';
}
} catch (e) {
cli.fatal('daemon.node not installed. Please run `npm install daemon`');
}
break;
case 'catchall':
process.on('uncaughtException', function (err) {
cli.error('Uncaught exception: ' + (err.msg || err));
Expand Down Expand Up @@ -347,9 +336,6 @@ cli.parse = function (opts, command_def) {
console.error(cli.app + ' v' + cli.version);
cli.exit();
break;
} else if (enable.daemon && (o === 'd' || o === 'daemon')) {
daemon_arg = cli.getArrayValue(['start','stop','restart','pid','log'], is_long ? null : 'start');
continue;
} else if (enable.catchall && (o === 'c' || o === 'catch')) {
continue;
} else if (enable.status && (o === 'k' || o === 'no-color')) {
Expand Down Expand Up @@ -667,9 +653,6 @@ cli.getUsage = function (code) {
if (enable.catchall && seen_opts.indexOf('c') === -1 && seen_opts.indexOf('catch') === -1) {
console.error(pad(' -c, --catch', switch_pad) + 'Catch unanticipated errors');
}
if (enable.daemon && seen_opts.indexOf('d') === -1 && seen_opts.indexOf('daemon') === -1) {
console.error(pad(' -d, --daemon [ARG]', switch_pad) + 'Daemonize the process. Control the daemon using [start, stop, restart, log, pid]');
}
if (enable.version && seen_opts.indexOf('v') === -1 && seen_opts.indexOf('version') === -1) {
console.error(pad(' -v, --version', switch_pad) + 'Display the current version');
}
Expand Down Expand Up @@ -965,102 +948,13 @@ cli.toType = function(obj) {
}

/**
* A method for creating and controlling a daemon.
*
* `arg` can be:
* start = daemonizes the process
* stop = stops the daemon if it is running
* restart = alias for stop -> start
* pid = outputs the daemon's PID if it is running
* log = outputs the daemon's log file (stdout + stderr)
*
* @param {String} arg (Optional - default is 'start')
* @param {Function} callback
* @api public
*/
cli.daemon = function (arg, callback) {
if (typeof daemon === 'undefined') {
cli.fatal('Daemon is not initialized');
}

if (typeof arg === 'function') {
callback = arg;
arg = 'start';
}

var lock_file = '/tmp/' + cli.app + '.pid',
log_file = '/tmp/' + cli.app + '.log';

var start = function () {
daemon.daemonize(log_file, lock_file, function (err) {
if (err) return cli.error('Error starting daemon: ' + err);
callback();
});
};

var stop = function () {
try {
cli.native.fs.readFileSync(lock_file);
} catch (e) {
return cli.error('Daemon is not running');
}
daemon.kill(lock_file, function (err, pid) {
if (err && err.errno === 3) {
return cli.error('Daemon is not running');
} else if (err) {
return cli.error('Error stopping daemon: ' + err.errno);
}
cli.ok('Successfully stopped daemon with pid: ' + pid);
});
};

switch(arg) {
case 'stop':
stop();
break;
case 'restart':
daemon.stop(lock_file, function () {
start();
});
break;
case 'log':
try {
cli.native.fs.createReadStream(log_file, {encoding: 'utf8'}).pipe(process.stdout);
} catch (e) {
return cli.error('No daemon log file');
}
break;
case 'pid':
try {
var pid = cli.native.fs.readFileSync(lock_file, 'utf8');
cli.native.fs.statSync('/proc/' + pid);
cli.info(pid);
} catch (e) {
return cli.error('Daemon is not running');
}
break;
default:
start();
break;
}
}

/**
* The main entry method. Calling cli.main() is only necessary in
* scripts that have daemon support enabled. `callback` receives (args, options)
* The main entry method. `callback` receives (args, options)
*
* @param {Function} callback
* @api public
*/
cli.main = function (callback) {
var after = function () {
callback.apply(cli, [cli.args, cli.options]);
};
if (enable.daemon && daemon_arg) {
cli.daemon(daemon_arg, after);
} else {
after();
}
callback.call(cli, cli.args, cli.options);
}

/**
Expand Down
27 changes: 0 additions & 27 deletions examples/static.coffee

This file was deleted.

25 changes: 0 additions & 25 deletions examples/static.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description" : "A tool for rapidly building command line apps",
"version" : "0.11.3",
"homepage" : "http://github.com/node-js-libs/cli",
"keywords" : ["cli","command line","opts","parseopt","opt","args","console","argsparse","optparse","daemon","autocomplete","command","autocompletion"],
"keywords" : ["cli","command line","opts","parseopt","opt","args","console","argsparse","optparse","autocomplete","command","autocompletion"],
"author" : "Chris O'Hara <cohara87@gmail.com>",
"main" : "cli.js",
"bugs": {
Expand Down

0 comments on commit ed90515

Please sign in to comment.