refactor.. usage updated..
This commit is contained in:
152
bin/ws.js
152
bin/ws.js
@ -15,27 +15,31 @@ var serveStatic = require("serve-static");
|
||||
var directory = require("serve-index");
|
||||
var compress = require("compression");
|
||||
var cliOptions = require("../lib/cli-options");
|
||||
var url = require("url");
|
||||
|
||||
/* specify the command line arg definitions and usage forms */
|
||||
var cli = cliArgs(cliOptions);
|
||||
var usage = cli.getUsage({
|
||||
title: "local-web-server",
|
||||
title: "[bold]{local-web-server}",
|
||||
description: "Lightweight static web server, zero configuration.",
|
||||
footer: "Project home: https://github.com/75lb/local-web-server",
|
||||
forms: [
|
||||
"$ ws <server options>",
|
||||
"$ ws --config",
|
||||
"$ ws --help"
|
||||
],
|
||||
usage: {
|
||||
title: "[bold]{Usage}",
|
||||
forms: [
|
||||
"$ ws <server options>",
|
||||
"$ ws --config",
|
||||
"$ ws --help"
|
||||
]
|
||||
},
|
||||
groups: {
|
||||
server: "Server",
|
||||
misc: "Misc"
|
||||
server: "[bold]{Server}",
|
||||
misc: "[bold]{Misc}"
|
||||
}
|
||||
});
|
||||
|
||||
/* parse command line args */
|
||||
try {
|
||||
var argv = cli.parse();
|
||||
var wsOptions = cli.parse();
|
||||
} catch(err){
|
||||
halt(err.message);
|
||||
}
|
||||
@ -59,20 +63,19 @@ var builtInDefaults = {
|
||||
};
|
||||
|
||||
/* override built-in defaults with stored config and then command line args */
|
||||
argv.server = o.extend(builtInDefaults, storedConfig, argv.server);
|
||||
wsOptions.server = o.extend(builtInDefaults, storedConfig, wsOptions.server);
|
||||
|
||||
/* user input validation */
|
||||
var logFormat = argv.server["log-format"];
|
||||
if (!t.isNumber(argv.server.port)) {
|
||||
if (!t.isNumber(wsOptions.server.port)) {
|
||||
halt("please supply a numeric port value");
|
||||
}
|
||||
|
||||
if (argv.misc.config){
|
||||
if (wsOptions.misc.config){
|
||||
dope.log("Stored config: ");
|
||||
dope.log(storedConfig);
|
||||
process.exit(0);
|
||||
|
||||
} else if (argv.misc.help){
|
||||
} else if (wsOptions.misc.help){
|
||||
dope.log(usage);
|
||||
|
||||
} else {
|
||||
@ -82,65 +85,14 @@ if (argv.misc.config){
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
var app = connect();
|
||||
|
||||
/* enable cross-origin requests on all resources */
|
||||
app.use(function(req, res, next){
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
next();
|
||||
});
|
||||
|
||||
/* log using --log-format (if supplied) */
|
||||
if(logFormat) {
|
||||
if (logFormat === "none"){
|
||||
// do nothing, no logging required
|
||||
} else {
|
||||
if (logFormat === "logstalgia"){
|
||||
/* customised logger :date token, purely to satisfy Logstalgia. */
|
||||
morgan.token("date", function(){
|
||||
var a = new Date();
|
||||
return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
|
||||
.replace("GMT", "").replace(" (BST)", "");
|
||||
});
|
||||
logFormat = "combined";
|
||||
}
|
||||
|
||||
app.use(morgan(logFormat));
|
||||
}
|
||||
|
||||
/* if no `--log-format` was specified, pipe the default format output
|
||||
into `log-stats`, which prints statistics to the console */
|
||||
} else {
|
||||
dope.hideCursor();
|
||||
app.use(morgan("common", { stream: logStats({ refreshRate: argv.server["refresh-rate"] }) }));
|
||||
}
|
||||
|
||||
/* --compress enables compression */
|
||||
if (argv.server.compress) app.use(compress());
|
||||
|
||||
/* set the mime-type overrides specified in the config */
|
||||
serveStatic.mime.define(argv.server.mime);
|
||||
|
||||
/* enable static file server, including directory browsing support */
|
||||
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.server.port + " is already is use");
|
||||
} else {
|
||||
halt(err.message);
|
||||
}
|
||||
})
|
||||
.listen(argv.server.port);
|
||||
dope.hideCursor();
|
||||
launchServer();
|
||||
|
||||
/* write launch information to stderr (stdout is reserved for web log output) */
|
||||
if (path.resolve(argv.server.directory) === process.cwd()){
|
||||
dope.error("serving at %underline{%s}", "http://localhost:" + argv.server.port);
|
||||
if (path.resolve(wsOptions.server.directory) === process.cwd()){
|
||||
dope.error("serving at %underline{%s}", "http://localhost:" + wsOptions.server.port);
|
||||
} else {
|
||||
dope.error("serving %underline{%s} at %underline{%s}", argv.server.directory, "http://localhost:" + argv.server.port);
|
||||
dope.error("serving %underline{%s} at %underline{%s}", wsOptions.server.directory, "http://localhost:" + wsOptions.server.port);
|
||||
}
|
||||
}
|
||||
|
||||
@ -149,3 +101,63 @@ function halt(message){
|
||||
dope.log(usage);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function launchServer(){
|
||||
var app = connect();
|
||||
|
||||
/* enable cross-origin requests on all resources */
|
||||
app.use(function(req, res, next){
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(getLogger());
|
||||
|
||||
/* --compress enables compression */
|
||||
if (wsOptions.server.compress) app.use(compress());
|
||||
|
||||
/* set the mime-type overrides specified in the config */
|
||||
serveStatic.mime.define(wsOptions.server.mime);
|
||||
|
||||
/* enable static file server, including directory browsing support */
|
||||
app.use(serveStatic(path.resolve(wsOptions.server.directory)))
|
||||
.use(directory(path.resolve(wsOptions.server.directory), { icons: true }));
|
||||
|
||||
/* launch server */
|
||||
http.createServer(app)
|
||||
.on("error", function(err){
|
||||
if (err.code === "EADDRINUSE"){
|
||||
halt("port " + wsOptions.server.port + " is already is use");
|
||||
} else {
|
||||
halt(err.message);
|
||||
}
|
||||
})
|
||||
.listen(wsOptions.server.port);
|
||||
}
|
||||
|
||||
function getLogger(){
|
||||
/* log using --log-format (if supplied) */
|
||||
var logFormat = wsOptions.server["log-format"];
|
||||
if(logFormat) {
|
||||
if (logFormat === "none"){
|
||||
// do nothing, no logging required
|
||||
} else {
|
||||
if (logFormat === "logstalgia"){
|
||||
/* customised logger :date token, purely to satisfy Logstalgia. */
|
||||
morgan.token("date", function(){
|
||||
var d = new Date();
|
||||
return (d.getDate() + "/" + d.getUTCMonth() + "/" + d.getFullYear() + ":" + d.toTimeString())
|
||||
.replace("GMT", "").replace(" (BST)", "");
|
||||
});
|
||||
logFormat = "combined";
|
||||
}
|
||||
|
||||
return morgan(logFormat);
|
||||
}
|
||||
|
||||
/* if no `--log-format` was specified, pipe the default format output
|
||||
into `log-stats`, which prints statistics to the console */
|
||||
} else {
|
||||
return morgan("common", { stream: logStats({ refreshRate: wsOptions.server["refresh-rate"] }) });
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
"repository": "https://github.com/75lb/local-web-server",
|
||||
"author": "Lloyd Brookes",
|
||||
"dependencies": {
|
||||
"command-line-args": "^1.0.0-0",
|
||||
"command-line-args": "^1.1.0-0",
|
||||
"compression": "^1.0.2",
|
||||
"config-master": "^1",
|
||||
"connect": "^3.0.0",
|
||||
@ -23,6 +23,7 @@
|
||||
"home-path": "^1",
|
||||
"morgan": "^1.0.0",
|
||||
"object-tools": "^2",
|
||||
"proxy-middleware": "^0.13.0",
|
||||
"serve-index": "^1.6.3",
|
||||
"serve-static": "^1.8",
|
||||
"stream-log-stats": "^1",
|
||||
|
Reference in New Issue
Block a user