Browse Source

error handling

master
Lloyd Brookes 11 years ago
parent
commit
865845c4b5
  1. 2
      README.md
  2. 38
      bin/ws.js
  3. 2
      package.json

2
README.md

@ -1,6 +1,6 @@
[![view on npm](http://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server) [![view on npm](http://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
![npm module downloads per month](http://img.shields.io/npm/dm/local-web-server.svg) ![npm module downloads per month](http://img.shields.io/npm/dm/local-web-server.svg)
[![Dependency Status](https://david-dm.org/75lb/local-web-server.png)](https://david-dm.org/75lb/local-web-server)
[![Dependency Status](https://david-dm.org/75lb/local-web-server.svg)](https://david-dm.org/75lb/local-web-server)
![Analytics](https://ga-beacon.appspot.com/UA-27725889-12/local-web-server/README.md?pixel) ![Analytics](https://ga-beacon.appspot.com/UA-27725889-12/local-web-server/README.md?pixel)
local-web-server local-web-server

38
bin/ws.js

@ -3,17 +3,20 @@
var dope = require("console-dope"), var dope = require("console-dope"),
connect = require("connect"), connect = require("connect"),
http = require("http"), http = require("http"),
fs = require("fs"),
Model = require("nature").Model, Model = require("nature").Model,
w = require("wodge"), w = require("wodge"),
path = require("path"), path = require("path"),
loadConfig = require("config-master"), loadConfig = require("config-master"),
morgan = require("morgan"), morgan = require("morgan"),
serveStatic = require("serve-static"), serveStatic = require("serve-static"),
directory = require('serve-index'),
compress = require('compression');
directory = require("serve-index"),
compress = require("compression");
var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny] [--compress|-c]";
var usage =
"usage: \n\
$ ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny]\n\
$ ws --compress|-c \n\
$ ws --help|-h";
function halt(message){ function halt(message){
dope.red.log("Error: %s", message); dope.red.log("Error: %s", message);
@ -27,10 +30,8 @@ var argv = new Model()
.define({ name: "log-format", alias: "f", type: "string" }) .define({ name: "log-format", alias: "f", type: "string" })
.define({ name: "help", alias: "h", type: "boolean" }) .define({ name: "help", alias: "h", type: "boolean" })
.define({ name: "directory", alias: "d", type: "string", value: process.cwd() }) .define({ name: "directory", alias: "d", type: "string", value: process.cwd() })
.define({ name: "compress", alias: "c", type: "boolean" })
.on("error", function(err){
halt(err.message);
});
.define({ name: "config", type: "boolean" })
.define({ name: "compress", alias: "c", type: "boolean" });
/* Merge together options from "~/.local-web-server.json", "{cwd}/.local-web-server.json" and "{cwd}/package.json", in that order. */ /* Merge together options from "~/.local-web-server.json", "{cwd}/.local-web-server.json" and "{cwd}/package.json", in that order. */
var storedConfig = loadConfig( var storedConfig = loadConfig(
@ -38,15 +39,30 @@ var storedConfig = loadConfig(
path.join(process.cwd(), ".local-web-server.json"), path.join(process.cwd(), ".local-web-server.json"),
path.join(process.cwd(), "package.json:local-web-server") path.join(process.cwd(), "package.json:local-web-server")
); );
argv.set(storedConfig);
try {
argv.set(storedConfig);
} catch (err){
dope.red.log("Failed to set stored config, please check your config files");
halt(err.message);
}
/* Finally, set the options from the command-line, overriding all defaults. */ /* Finally, set the options from the command-line, overriding all defaults. */
argv.set(process.argv);
try {
argv.set(process.argv);
} catch (err){
dope.red.log("Failed to set command line options");
halt(err.message);
}
/* Die here if invalid args received */ /* Die here if invalid args received */
if (!argv.valid) halt(argv.validationMessages); if (!argv.valid) halt(argv.validationMessages);
if (argv.help){
if (argv.config){
dope.log("Stored config: ");
dope.log(storedConfig);
process.exit(0);
} else if (argv.help){
dope.log(usage); dope.log(usage);
} else { } else {

2
package.json

@ -18,7 +18,7 @@
"nature": "~0.4.0", "nature": "~0.4.0",
"console-dope": "~0.3.0", "console-dope": "~0.3.0",
"wodge": "~0.5.0", "wodge": "~0.5.0",
"config-master": "~0.0.0",
"config-master": "~0.1.0",
"morgan": "^1.0.0", "morgan": "^1.0.0",
"serve-static": "^1.1.0", "serve-static": "^1.1.0",
"serve-index": "^1.0.2", "serve-index": "^1.0.2",

Loading…
Cancel
Save