Permalink
Browse files

No more CLI class. Add exception handler.

  • Loading branch information...
1 parent fe2e76f commit 81e3ba00bd819ea4cc2872a3bad8b1335c0fecf2 @reid reid committed Dec 16, 2011
Showing with 74 additions and 36 deletions.
  1. +1 −5 cli.js
  2. +73 −31 lib/cli.js
View
6 cli.js
@@ -1,6 +1,2 @@
#!/usr/bin/env node
-var CLI = require("./lib/cli");
-
-var interface = new CLI();
-
-interface.route(process.argv);
+require("./lib/cli").route(process.argv);
View
104 lib/cli.js
@@ -1,19 +1,59 @@
var nopt = require("nopt");
-var winston = require("winston");
-var pkg = require("./package");
+var util = require("util");
+
+var meta = require("./package").readPackageSync();
+
var Hub = require("./hub");
+var color = require("./old/color").codes;
+
+var good = "";
+var bad = "";
+
+function error () {
+ var args = Array.prototype.slice.apply(arguments);
+ util.error.apply(util, args);
+}
+
+function panic () {
+ var args = Array.prototype.slice.apply(arguments);
+ error.apply(this, args);
+ process.exit(1);
+}
+
+function puts () {
+ var args = Array.prototype.slice.apply(arguments);
+ util.puts.apply(util, args);
+}
+
+function setupProcess () {
+ process.on("uncaughtException", function (err) {
+ if ("string" !== typeof err) {
+ err = err.stack;
+ }
-var parseArgv = function (argv) {
+ panic([
+ color.red(bad + " Fatal error") + ": " + err, "",
+ "If you believe this is a bug in Yeti, please report it.",
+ " " + color.bold(meta.bugs.url),
+ " Yeti v" + meta.version,
+ " Node.js " + process.version
+ ].join("\n"));
+ });
+}
+
+function parseArgv (argv) {
var knownOptions = {
"server": Boolean,
"version": Boolean,
+ "debug": Boolean,
"port": Number,
"help" : Boolean
};
var shortHands = {
"s": ["--server"],
+ "d": ["--debug"],
"p": ["--port"],
"v": ["--version"]
};
@@ -23,43 +63,45 @@ var parseArgv = function (argv) {
return nopt(knownOptions, shortHands, argv);
};
-var CLI = module.exports = function CLI () {
- this.log = new winston.Logger({
- transports: [
- new (winston.transports.Console)()
- ]
- });
- this.log.cli();
- this.log.extend(this);
-}
+function runBatch (files) {
+ error("Looks like you'd like to run some files: " + files.join(", "));
+ error("Not implemented yet.");
+};
+
+exports.route = function (argv) {
+ setupProcess();
-CLI.prototype.route = function (argv) {
var options = parseArgv(argv);
- var self = this;
- function printUsage () {
- self.info("usage: " + argv[1] +
+ var usage = "usage: " + argv[1] +
" [--version | -v] [--server | -s] [--port=<n>]" +
- " [--help] [--] [<HTML files>]");
- }
+ " [--help] [--] [<HTML files>]";
- if (options.server) {
- var server = new Hub();
+ if (options.argv.remain.length) {
+ if (options.server) {
+ error("Ignoring --server option.");
+ }
+ runBatch(options.argv.remain);
+ } else if (options.server) {
+ var server = new Hub({
+ log: {
+ console: {
+ silent: !options.debug
+ }
+ }
+ });
server.listen(8090, function () {
- self.info("Yeti Hub listening on port 8090.");
+ error("Yeti Hub listening on port 8090.");
});
- } else if (options.argv.remain.length) {
- var files = options.argv.remain;
- self.info("Looks like you'd like to run some files:" + files.join(", "));
- self.warn("Not implemented yet.");
} else if (options.version) {
- self.info(pkg.readPackageSync().version);
+ puts(meta.version);
} else if (options.help) {
- printUsage();
+ puts(usage);
} else {
- printUsage();
- self.info("No files specified. To launch the Yeti server, specify --server.");
- process.exit(1);
+ panic(
+ "No files specified. " +
+ "To launch the Yeti server, specify --server.\n" +
+ usage
+ );
}
};
-

0 comments on commit 81e3ba0

Please sign in to comment.