new usage
This commit is contained in:
72
bin/ws.js
72
bin/ws.js
@ -12,18 +12,23 @@ var dope = require("console-dope"),
|
|||||||
morgan = require("morgan"),
|
morgan = require("morgan"),
|
||||||
serveStatic = require("serve-static"),
|
serveStatic = require("serve-static"),
|
||||||
directory = require("serve-index"),
|
directory = require("serve-index"),
|
||||||
compress = require("compression");
|
compress = require("compression"),
|
||||||
|
cliOptions = require("../lib/cli-options");
|
||||||
|
|
||||||
var usage =
|
var cli = cliArgs(cliOptions);
|
||||||
"usage: \n\
|
var usage = cli.usage({
|
||||||
$ ws [--directory|-d <dir>] [--port|-p <port>] [--log-format|-f node|dev|default|short|tiny|logstalgia] [--compress|-c]\n\
|
forms: [
|
||||||
$ ws --config\n\
|
"$ ws <server options>",
|
||||||
$ ws --help|-h";
|
"$ ws --config",
|
||||||
|
"$ ws --help"
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
function halt(message){
|
/* parse command line args */
|
||||||
dope.red.log("Error: %s", message);
|
try {
|
||||||
dope.log(usage);
|
var argv = cli.parse();
|
||||||
process.exit(1);
|
} catch(err){
|
||||||
|
halt(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Load and merge together options from
|
/* Load and merge together options from
|
||||||
@ -37,21 +42,6 @@ var storedConfig = loadConfig(
|
|||||||
{ jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" }
|
{ 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 = {
|
var builtInDefaults = {
|
||||||
port: 8000,
|
port: 8000,
|
||||||
directory: process.cwd(),
|
directory: process.cwd(),
|
||||||
@ -59,14 +49,14 @@ var builtInDefaults = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* override built-in defaults with stored config and then command line args */
|
/* 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("Stored config: ");
|
||||||
dope.log(storedConfig);
|
dope.log(storedConfig);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
|
|
||||||
} else if (argv.help){
|
} else if (argv.Misc.help){
|
||||||
dope.log(usage);
|
dope.log(usage);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -79,7 +69,7 @@ if (argv.config){
|
|||||||
var app = connect();
|
var app = connect();
|
||||||
|
|
||||||
/* log using --log-format (if supplied) */
|
/* log using --log-format (if supplied) */
|
||||||
var logFormat = argv["log-format"];
|
var logFormat = argv.Server["log-format"];
|
||||||
if(logFormat) {
|
if(logFormat) {
|
||||||
if (logFormat === "none"){
|
if (logFormat === "none"){
|
||||||
// do nothing, no logging required
|
// do nothing, no logging required
|
||||||
@ -100,31 +90,37 @@ if (argv.config){
|
|||||||
/* if no `--log-format` was specified, pipe the default format output
|
/* if no `--log-format` was specified, pipe the default format output
|
||||||
into `log-stats`, which prints statistics to the console */
|
into `log-stats`, which prints statistics to the console */
|
||||||
} else {
|
} else {
|
||||||
app.use(morgan({ stream: logStats({ refreshRate: argv.refreshRate }) }));
|
app.use(morgan({ stream: logStats({ refreshRate: argv.Server.refreshRate }) }));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --compress enables compression */
|
/* --compress enables compression */
|
||||||
if (argv.compress) app.use(compress());
|
if (argv.Server.compress) app.use(compress());
|
||||||
|
|
||||||
/* static file server including directory browsing support */
|
/* static file server including directory browsing support */
|
||||||
app.use(serveStatic(path.resolve(argv.directory)))
|
app.use(serveStatic(path.resolve(argv.Server.directory)))
|
||||||
.use(directory(path.resolve(argv.directory), { icons: true }));
|
.use(directory(path.resolve(argv.Server.directory), { icons: true }));
|
||||||
|
|
||||||
/* launch server */
|
/* launch server */
|
||||||
http.createServer(app)
|
http.createServer(app)
|
||||||
.on("error", function(err){
|
.on("error", function(err){
|
||||||
if (err.code === "EADDRINUSE"){
|
if (err.code === "EADDRINUSE"){
|
||||||
halt("port " + argv.port + " is already is use");
|
halt("port " + argv.Server.port + " is already is use");
|
||||||
} else {
|
} else {
|
||||||
halt(err.message);
|
halt(err.message);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.listen(argv.port);
|
.listen(argv.Server.port);
|
||||||
|
|
||||||
/* write launch information to stderr (stdout is reserved for web log output) */
|
/* write launch information to stderr (stdout is reserved for web log output) */
|
||||||
if (path.resolve(argv.directory) === process.cwd()){
|
if (path.resolve(argv.Server.directory) === process.cwd()){
|
||||||
dope.error("serving at %underline{%s}", "http://localhost:" + argv.port);
|
dope.error("serving at %underline{%s}", "http://localhost:" + argv.Server.port);
|
||||||
} else {
|
} 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);
|
||||||
|
}
|
||||||
|
40
lib/cli-options.js
Normal file
40
lib/cli-options.js
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
@ -14,7 +14,7 @@
|
|||||||
"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.2.0",
|
"command-line-args": "^0.3.0",
|
||||||
"compression": "^1.0.2",
|
"compression": "^1.0.2",
|
||||||
"config-master": "~0.2.0",
|
"config-master": "~0.2.0",
|
||||||
"connect": "^3.0.0",
|
"connect": "^3.0.0",
|
||||||
|
Reference in New Issue
Block a user