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

128 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"),
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"),
2014-06-16 22:42:21 +01:00
homePath = require("home-path"),
logStats = require("stream-log-stats"),
connect = require("connect"),
morgan = require("morgan"),
serveStatic = require("serve-static"),
2014-05-08 15:05:06 +02:00
directory = require("serve-index"),
2014-06-17 00:40:41 +01:00
compress = require("compression"),
cliOptions = require("../lib/cli-options");
2014-06-17 09:52:26 +01:00
/* specify the command line arg definitions and usage forms */
2014-06-17 00:40:41 +01:00
var cli = cliArgs(cliOptions);
var usage = cli.usage({
forms: [
"$ ws <server options>",
"$ ws --config",
"$ ws --help"
]
});
2013-06-04 12:45:30 +01:00
2014-06-17 00:40:41 +01:00
/* parse command line args */
try {
var argv = cli.parse();
} catch(err){
halt(err.message);
2014-02-12 10:52:07 +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
- 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
2014-06-16 22:42:21 +01:00
var builtInDefaults = {
port: 8000,
directory: process.cwd(),
refreshRate: 500
2014-06-16 22:42:21 +01:00
};
/* override built-in defaults with stored config and then command line args */
2014-06-17 00:40:41 +01:00
argv.Server = o.extend(builtInDefaults, storedConfig, argv.Server);
2014-02-09 08:40:32 +01:00
2014-06-17 00:40:41 +01:00
if (argv.Misc.config){
2014-05-08 15:05:06 +02:00
dope.log("Stored config: ");
dope.log(storedConfig);
process.exit(0);
2014-06-17 00:40:41 +01:00
} else if (argv.Misc.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) */
2014-06-17 00:40:41 +01:00
var logFormat = argv.Server["log-format"];
2014-06-16 22:42:21 +01:00
if(logFormat) {
if (logFormat === "none"){
2014-06-15 19:43:35 +01:00
// do nothing, no logging required
} else {
2014-06-16 22:42:21 +01:00
if (logFormat === "logstalgia"){
2014-06-15 19:43:35 +01:00
/* 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)", "");
});
2014-06-16 22:42:21 +01:00
logFormat = "default";
2014-06-15 19:43:35 +01:00
}
2014-06-16 22:42:21 +01:00
app.use(morgan(logFormat));
2014-06-15 19:43:35 +01:00
}
2014-06-16 22:42:21 +01:00
/* if no `--log-format` was specified, pipe the default format output
into `log-stats`, which prints statistics to the console */
} else {
2014-06-17 00:40:41 +01:00
app.use(morgan({ stream: logStats({ refreshRate: argv.Server.refreshRate }) }));
2014-02-10 21:13:13 +01:00
}
/* --compress enables compression */
2014-06-17 00:40:41 +01:00
if (argv.Server.compress) app.use(compress());
2014-02-17 10:20:32 +00:00
2014-06-17 09:52:26 +01:00
/* enable static file server, including directory browsing support */
2014-06-17 00:40:41 +01:00
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"){
2014-06-17 00:40:41 +01:00
halt("port " + argv.Server.port + " is already is use");
} else {
halt(err.message);
}
})
2014-06-17 00:40:41 +01:00
.listen(argv.Server.port);
2013-06-15 00:41:43 +01:00
2014-06-16 22:42:21 +01:00
/* write launch information to stderr (stdout is reserved for web log output) */
2014-06-17 00:40:41 +01:00
if (path.resolve(argv.Server.directory) === process.cwd()){
dope.error("serving at %underline{%s}", "http://localhost:" + argv.Server.port);
2014-02-12 10:52:07 +01:00
} else {
2014-06-17 00:40:41 +01:00
dope.error("serving %underline{%s} at %underline{%s}", argv.Server.directory, "http://localhost:" + argv.Server.port);
2014-02-12 10:52:07 +01:00
}
2013-06-15 00:41:43 +01:00
}
2014-06-17 00:40:41 +01:00
function halt(message){
dope.red.log("Error: %s", message);
dope.log(usage);
process.exit(1);
}