Files
hiring-test-one/bin/ws.js

124 lines
3.9 KiB
JavaScript
Raw Normal View History

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"),
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"),
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\
$ 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);
}
/* Load and merge together options from
2014-05-29 23:01:39 +01:00
- ~/.local-web-server.json
- {cwd}/.local-web-server.json
- the `local-web-server` property of {cwd}/package.json
2014-05-29 23:01:39 +01:00
*/
var storedConfig = loadConfig(
2014-06-07 22:21:51 +01:00
path.join(homePath(), ".local-web-server.json"),
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-02-26 15:23:16 +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 },
{ name: "directory", alias: "d", type: String },
2014-05-29 23:01:39 +01:00
{ name: "config", type: Boolean },
{ 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);
}
/* 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 {
process.on("SIGINT", function(){
2014-03-12 14:34:52 +01:00
dope.showCursor();
dope.log();
process.exit(0);
});
2014-02-10 21:13:13 +01:00
var app = connect();
2014-06-12 19:37:12 +01:00
/* log using --log-format (if supplied) */
if(argv["log-format"]) {
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";
}
app.use(morgan(argv["log-format"]));
/* if no specific `--log-format` required, pipe the default web log output
into `log-stats`, which prints statistics to the console */
} else {
app.use(morgan({ stream: logStats({ refreshRate: argv.refreshRate }) }));
2014-02-10 21:13:13 +01:00
}
/* --compress enables compression */
if (argv.compress) app.use(compress());
2014-02-17 10:20:32 +00:00
/* static file server including directory browsing support */
app.use(serveStatic(path.resolve(argv.directory)))
.use(directory(path.resolve(argv.directory), { icons: true }));
/* launch server */
http.createServer(app)
.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
/* write status to stderr (stdout is reserved for web log output) */
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
}