From fb5839f96f0b6fbb3fa350eacf2169d41823083e Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Tue, 17 Jun 2014 00:40:41 +0100 Subject: [PATCH] new usage --- bin/ws.js | 74 ++++++++++++++++++++++++++---------------------------- lib/cli-options.js | 40 +++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 lib/cli-options.js diff --git a/bin/ws.js b/bin/ws.js index 82c2c29..51f95b0 100755 --- a/bin/ws.js +++ b/bin/ws.js @@ -12,18 +12,23 @@ var dope = require("console-dope"), morgan = require("morgan"), serveStatic = require("serve-static"), directory = require("serve-index"), - compress = require("compression"); + compress = require("compression"), + cliOptions = require("../lib/cli-options"); + +var cli = cliArgs(cliOptions); +var usage = cli.usage({ + forms: [ + "$ ws ", + "$ ws --config", + "$ ws --help" + ] +}); -var usage = -"usage: \n\ -$ ws [--directory|-d ] [--port|-p ] [--log-format|-f node|dev|default|short|tiny|logstalgia] [--compress|-c]\n\ -$ ws --config\n\ -$ ws --help|-h"; - -function halt(message){ - dope.red.log("Error: %s", message); - dope.log(usage); - process.exit(1); +/* parse command line args */ +try { + var argv = cli.parse(); +} catch(err){ + halt(err.message); } /* Load and merge together options from @@ -37,21 +42,6 @@ var storedConfig = loadConfig( { jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" } ); -/* parse command line args */ -try { - var argv = cliArgs([ - { name: "port", alias: "p", type: Number, defaultOption: true }, - { name: "log-format", alias: "f", type: String }, - { name: "help", alias: "h", type: Boolean }, - { name: "directory", alias: "d", type: String }, - { name: "config", type: Boolean }, - { name: "compress", alias: "c", type: Boolean }, - { name: "refreshRate", alias: "r", type: Number } - ]).parse(); -} catch(err){ - halt(err.message); -} - var builtInDefaults = { port: 8000, directory: process.cwd(), @@ -59,14 +49,14 @@ var builtInDefaults = { }; /* override built-in defaults with stored config and then command line args */ -argv = o.extend(builtInDefaults, storedConfig, argv); +argv.Server = o.extend(builtInDefaults, storedConfig, argv.Server); -if (argv.config){ +if (argv.Misc.config){ dope.log("Stored config: "); dope.log(storedConfig); process.exit(0); -} else if (argv.help){ +} else if (argv.Misc.help){ dope.log(usage); } else { @@ -79,7 +69,7 @@ if (argv.config){ var app = connect(); /* log using --log-format (if supplied) */ - var logFormat = argv["log-format"]; + var logFormat = argv.Server["log-format"]; if(logFormat) { if (logFormat === "none"){ // do nothing, no logging required @@ -100,31 +90,37 @@ if (argv.config){ /* if no `--log-format` was specified, pipe the default format output into `log-stats`, which prints statistics to the console */ } else { - app.use(morgan({ stream: logStats({ refreshRate: argv.refreshRate }) })); + app.use(morgan({ stream: logStats({ refreshRate: argv.Server.refreshRate }) })); } /* --compress enables compression */ - if (argv.compress) app.use(compress()); + if (argv.Server.compress) app.use(compress()); /* static file server including directory browsing support */ - app.use(serveStatic(path.resolve(argv.directory))) - .use(directory(path.resolve(argv.directory), { icons: true })); + app.use(serveStatic(path.resolve(argv.Server.directory))) + .use(directory(path.resolve(argv.Server.directory), { icons: true })); /* launch server */ http.createServer(app) .on("error", function(err){ if (err.code === "EADDRINUSE"){ - halt("port " + argv.port + " is already is use"); + halt("port " + argv.Server.port + " is already is use"); } else { halt(err.message); } }) - .listen(argv.port); + .listen(argv.Server.port); /* write launch information to stderr (stdout is reserved for web log output) */ - if (path.resolve(argv.directory) === process.cwd()){ - dope.error("serving at %underline{%s}", "http://localhost:" + argv.port); + if (path.resolve(argv.Server.directory) === process.cwd()){ + dope.error("serving at %underline{%s}", "http://localhost:" + argv.Server.port); } else { - dope.error("serving %underline{%s} at %underline{%s}", argv.directory, "http://localhost:" + argv.port); + dope.error("serving %underline{%s} at %underline{%s}", argv.Server.directory, "http://localhost:" + argv.Server.port); } } + +function halt(message){ + dope.red.log("Error: %s", message); + dope.log(usage); + process.exit(1); +} diff --git a/lib/cli-options.js b/lib/cli-options.js new file mode 100644 index 0000000..bb64505 --- /dev/null +++ b/lib/cli-options.js @@ -0,0 +1,40 @@ +module.exports = [ + { + groups: "Server", + options: [ + { + name: "port", alias: "p", type: Number, defaultOption: true, + description: "Web server port" + }, + { + name: "log-format", alias: "f", type: String, + description: "Access log format, written to stdout. Use one of \n'none', 'dev', 'default', 'short', 'tiny' or 'logstalgia'" + }, + { + name: "directory", alias: "d", type: String, + description: "Root directory, defaults to the current directory" + }, + { + name: "compress", alias: "c", type: Boolean, + description: "Enables compression" + }, + { + name: "refreshRate", alias: "r", type: Number, + description: "Statistics view refresh rate in ms. Defaults to 500." + } + ] + }, + { + groups: "Misc", + options: [ + { + name: "help", alias: "h", type: Boolean, + description: "Print these usage instructions" + }, + { + name: "config", type: Boolean, + description: "Prints the stored config" + } + ] + } +]; diff --git a/package.json b/package.json index 434538e..b24596d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "repository": "https://github.com/75lb/local-web-server", "author": "Lloyd Brookes", "dependencies": { - "command-line-args": "^0.2.0", + "command-line-args": "^0.3.0", "compression": "^1.0.2", "config-master": "~0.2.0", "connect": "^3.0.0",