Browse Source

replaced nature with command-line-args

master
Lloyd Brookes 11 years ago
parent
commit
913f6573c5
  1. 41
      bin/ws.js
  2. 10
      package.json

41
bin/ws.js

@ -3,7 +3,7 @@
var dope = require("console-dope"), var dope = require("console-dope"),
connect = require("connect"), connect = require("connect"),
http = require("http"), http = require("http"),
Model = require("nature").Model,
parseArgv = require("command-line-args"),
w = require("wodge"), w = require("wodge"),
path = require("path"), path = require("path"),
loadConfig = require("config-master"), loadConfig = require("config-master"),
@ -14,7 +14,7 @@ var dope = require("console-dope"),
var usage = var usage =
"usage: \n\ "usage: \n\
$ ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny] [--compress|-c]\n\
$ ws [--directory|-d <dir>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny] [--compress|-c]\n\
$ ws --config\n\ $ ws --config\n\
$ ws --help|-h"; $ ws --help|-h";
@ -24,38 +24,31 @@ function halt(message){
process.exit(1); process.exit(1);
} }
/* parse command-line args */
var argv = new Model()
.define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
.define({ name: "log-format", alias: "f", type: "string" })
.define({ name: "help", alias: "h", type: "boolean" })
.define({ name: "directory", alias: "d", type: "string", value: process.cwd() })
.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
- {cwd}/package.json
*/
var storedConfig = loadConfig( var storedConfig = loadConfig(
path.join(w.getHomeDir(), ".local-web-server.json"), path.join(w.getHomeDir(), ".local-web-server.json"),
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")
); );
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. */
/* override stored config with values parsed from command line */
try { try {
argv.set(process.argv);
var argv = parseArgv([
{ name: "port", alias: "p", type: Number, defaultOption: true, value: 8000 },
{ name: "log-format", alias: "f", type: String },
{ name: "help", alias: "h", type: Boolean },
{ name: "directory", alias: "d", type: String, value: process.cwd() },
{ name: "config", type: Boolean },
{ name: "compress", alias: "c", type: Boolean }
]);
} catch(err){ } catch(err){
dope.red.log("Failed to set command line options");
halt(err.message); halt(err.message);
} }
/* Die here if invalid args received */
if (!argv.valid) halt(argv.validationMessages);
argv = w.extend(storedConfig, argv);
if (argv.config){ if (argv.config){
dope.log("Stored config: "); dope.log("Stored config: ");

10
package.json

@ -14,14 +14,14 @@
"repository": "https://github.com/75lb/local-web-server", "repository": "https://github.com/75lb/local-web-server",
"author": "Lloyd Brookes", "author": "Lloyd Brookes",
"dependencies": { "dependencies": {
"command-line-args": "~0.0.2",
"compression": "^1.0.2",
"config-master": "~0.1.0",
"connect": "^3.0.0", "connect": "^3.0.0",
"nature": "~0.4.0",
"console-dope": "~0.3.0", "console-dope": "~0.3.0",
"wodge": "~0.5.0",
"config-master": "~0.1.0",
"morgan": "^1.0.0", "morgan": "^1.0.0",
"serve-static": "^1.1.0",
"serve-index": "^1.0.2", "serve-index": "^1.0.2",
"compression": "^1.0.2"
"serve-static": "^1.1.0",
"wodge": "~0.6.0"
} }
} }
Loading…
Cancel
Save