2014-02-05 15:28:23 +01:00
|
|
|
#!/usr/bin/env node
|
2014-02-12 10:52:07 +01:00
|
|
|
"use strict";
|
2014-03-12 14:34:52 +01:00
|
|
|
var dope = require("console-dope"),
|
|
|
|
connect = require("connect"),
|
2014-01-03 18:04:44 +00:00
|
|
|
http = require("http"),
|
2014-06-04 08:54:37 +01:00
|
|
|
cliArgs = require("command-line-args"),
|
2014-06-13 21:45:50 +01:00
|
|
|
o = require("object-tools"),
|
2014-05-01 11:28:15 +01:00
|
|
|
path = require("path"),
|
|
|
|
loadConfig = require("config-master"),
|
|
|
|
morgan = require("morgan"),
|
|
|
|
serveStatic = require("serve-static"),
|
2014-05-08 15:05:06 +02:00
|
|
|
directory = require("serve-index"),
|
2014-06-07 22:21:51 +01:00
|
|
|
compress = require("compression"),
|
|
|
|
homePath = require("home-path"),
|
2014-06-12 19:37:12 +01:00
|
|
|
logStats = require("stream-log-stats");
|
2013-06-04 12:45:30 +01:00
|
|
|
|
2014-05-08 15:05:06 +02:00
|
|
|
var usage =
|
|
|
|
"usage: \n\
|
2014-06-13 18:42:35 +01:00
|
|
|
$ ws [--directory|-d <dir>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny|logstalgia] [--compress|-c]\n\
|
2014-05-08 16:07:42 +02:00
|
|
|
$ ws --config\n\
|
2014-05-08 15:05:06 +02:00
|
|
|
$ ws --help|-h";
|
2014-02-05 15:28:23 +01:00
|
|
|
|
2014-02-12 10:52:07 +01:00
|
|
|
function halt(message){
|
2014-03-12 14:34:52 +01:00
|
|
|
dope.red.log("Error: %s", message);
|
|
|
|
dope.log(usage);
|
2014-02-12 10:52:07 +01:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2014-06-15 18:43:05 +01:00
|
|
|
/* Load and merge together options from
|
2014-05-29 23:01:39 +01:00
|
|
|
- ~/.local-web-server.json
|
|
|
|
- {cwd}/.local-web-server.json
|
2014-06-15 18:43:05 +01:00
|
|
|
- the `local-web-server` property of {cwd}/package.json
|
2014-05-29 23:01:39 +01:00
|
|
|
*/
|
2014-05-01 11:28:15 +01:00
|
|
|
var storedConfig = loadConfig(
|
2014-06-07 22:21:51 +01:00
|
|
|
path.join(homePath(), ".local-web-server.json"),
|
2014-05-01 11:28:15 +01:00
|
|
|
path.join(process.cwd(), ".local-web-server.json"),
|
2014-06-13 17:20:25 +01:00
|
|
|
{ jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" }
|
2014-05-01 11:28:15 +01:00
|
|
|
);
|
2014-02-26 15:23:16 +01:00
|
|
|
|
2014-06-15 18:43:05 +01:00
|
|
|
/* parse command line args */
|
2014-05-08 15:05:06 +02:00
|
|
|
try {
|
2014-06-04 08:54:37 +01:00
|
|
|
var argv = cliArgs([
|
2014-06-14 07:03:41 +01:00
|
|
|
{ name: "port", alias: "p", type: Number, defaultOption: true },
|
2014-05-29 23:01:39 +01:00
|
|
|
{ name: "log-format", alias: "f", type: String },
|
|
|
|
{ name: "help", alias: "h", type: Boolean },
|
2014-06-15 18:43:05 +01:00
|
|
|
{ name: "directory", alias: "d", type: String },
|
2014-05-29 23:01:39 +01:00
|
|
|
{ name: "config", type: Boolean },
|
2014-06-15 18:43:05 +01:00
|
|
|
{ name: "compress", alias: "c", type: Boolean },
|
|
|
|
{ name: "refreshRate", alias: "r", type: Number }
|
2014-06-04 08:54:37 +01:00
|
|
|
]).parse();
|
2014-05-29 23:01:39 +01:00
|
|
|
} catch(err){
|
2014-05-08 15:05:06 +02:00
|
|
|
halt(err.message);
|
|
|
|
}
|
2014-06-15 18:43:05 +01:00
|
|
|
|
|
|
|
/* override built-in defaults with stored config then command line args */
|
|
|
|
argv = o.extend({
|
|
|
|
port: 8000,
|
|
|
|
directory: process.cwd(),
|
|
|
|
refreshRate: 500
|
|
|
|
}, storedConfig, argv);
|
2014-02-09 08:40:32 +01:00
|
|
|
|
2014-05-08 15:05:06 +02:00
|
|
|
if (argv.config){
|
|
|
|
dope.log("Stored config: ");
|
|
|
|
dope.log(storedConfig);
|
|
|
|
process.exit(0);
|
|
|
|
|
|
|
|
} else if (argv.help){
|
2014-03-12 14:34:52 +01:00
|
|
|
dope.log(usage);
|
2013-06-15 00:41:43 +01:00
|
|
|
|
|
|
|
} else {
|
2014-02-12 13:22:30 +01:00
|
|
|
process.on("SIGINT", function(){
|
2014-03-12 14:34:52 +01:00
|
|
|
dope.showCursor();
|
|
|
|
dope.log();
|
2014-02-12 13:22:30 +01:00
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
|
2014-02-10 21:13:13 +01:00
|
|
|
var app = connect();
|
2014-02-12 13:22:30 +01:00
|
|
|
|
2014-06-12 19:37:12 +01:00
|
|
|
/* log using --log-format (if supplied) */
|
|
|
|
if(argv["log-format"]) {
|
2014-06-13 18:42:35 +01:00
|
|
|
if (argv["log-format"] === "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)", "");
|
|
|
|
});
|
|
|
|
argv["log-format"] = "default";
|
|
|
|
}
|
2014-06-15 18:43:05 +01:00
|
|
|
|
2014-05-01 11:28:15 +01:00
|
|
|
app.use(morgan(argv["log-format"]));
|
2014-06-15 18:43:05 +01:00
|
|
|
|
2014-06-14 15:46:27 +01:00
|
|
|
/* if no specific `--log-format` required, pipe the default web log output
|
|
|
|
into `log-stats`, which prints statistics to the console */
|
2014-02-13 14:23:17 +01:00
|
|
|
} else {
|
2014-06-15 18:43:05 +01:00
|
|
|
app.use(morgan({ stream: logStats({ refreshRate: argv.refreshRate }) }));
|
2014-02-10 21:13:13 +01:00
|
|
|
}
|
2014-02-12 13:22:30 +01:00
|
|
|
|
2014-05-01 11:28:15 +01:00
|
|
|
/* --compress enables compression */
|
|
|
|
if (argv.compress) app.use(compress());
|
2014-02-17 10:20:32 +00:00
|
|
|
|
2014-05-01 11:28:15 +01:00
|
|
|
/* static file server including directory browsing support */
|
|
|
|
app.use(serveStatic(path.resolve(argv.directory)))
|
|
|
|
.use(directory(path.resolve(argv.directory), { icons: true }));
|
2014-02-12 13:22:30 +01:00
|
|
|
|
2014-05-01 11:28:15 +01:00
|
|
|
/* launch server */
|
2014-06-15 18:43:05 +01:00
|
|
|
http.createServer(app)
|
2014-03-06 14:00:50 +01:00
|
|
|
.on("error", function(err){
|
|
|
|
if (err.code === "EADDRINUSE"){
|
|
|
|
halt("port " + argv.port + " is already is use");
|
|
|
|
} else {
|
|
|
|
halt(err.message);
|
|
|
|
}
|
|
|
|
})
|
2014-02-12 10:52:07 +01:00
|
|
|
.listen(argv.port);
|
2013-06-15 00:41:43 +01:00
|
|
|
|
2014-06-14 15:46:27 +01:00
|
|
|
/* write status to stderr (stdout is reserved for web log output) */
|
2014-03-06 14:00:50 +01:00
|
|
|
if (path.resolve(argv.directory) === process.cwd()){
|
2014-03-12 14:34:52 +01:00
|
|
|
dope.error("serving at %underline{%s}", "http://localhost:" + argv.port);
|
2014-02-12 10:52:07 +01:00
|
|
|
} else {
|
2014-03-12 14:34:52 +01:00
|
|
|
dope.error("serving %underline{%s} at %underline{%s}", argv.directory, "http://localhost:" + argv.port);
|
2014-02-12 10:52:07 +01:00
|
|
|
}
|
2013-06-15 00:41:43 +01:00
|
|
|
}
|