Files
hiring-test-one/ws.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-02-05 15:28:23 +01:00
#!/usr/bin/env node
var connect = require("connect"),
http = require("http"),
2014-02-01 23:10:18 +01:00
Thing = require("nature").Thing,
wodge = require("wodge");
2013-06-04 12:45:30 +01:00
var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
2014-02-05 15:28:23 +01:00
function halt(message){
2014-02-01 23:10:18 +01:00
console.log(wodge.red("Error ") + message);
console.log(usage);
process.exit(1);
}
2013-06-15 00:41:43 +01:00
var options = new Thing()
2014-02-01 23:10:18 +01:00
.define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
.define({ name: "log-format", alias: "f", type: "string", value: "dev" })
2013-06-15 00:41:43 +01:00
.define({ name: "help", alias: "h", type: "boolean" })
.define({ name: "directory", alias: "d", type: "string", defaultOption: true, value: process.cwd()})
.on("error", function(err){
halt(err.message);
})
2013-06-15 00:41:43 +01:00
.set(process.argv);
2013-06-13 13:19:52 +01:00
2013-06-15 00:41:43 +01:00
if (!options.valid){
2014-02-05 15:28:23 +01:00
halt(options.validationMessages);
2013-06-15 00:41:43 +01:00
} else if (options.help){
console.log(usage);
} else {
/**
2014-02-05 15:28:23 +01:00
customised connect.logger :date token, purely to satisfy Logstalgia.
2013-06-15 00:41:43 +01:00
*/
2014-02-05 15:28:23 +01:00
connect.logger.token("date", function(){
2013-06-15 00:41:43 +01:00
var a = new Date();
return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
.replace("GMT", "").replace(" (BST)", "");
});
var app = connect()
2013-06-15 00:41:43 +01:00
.use(connect.logger(options["log-format"]))
.use(connect.compress())
.use(connect.static(options.directory))
.use(connect.directory(options.directory, { icons: true }));
2014-02-05 15:28:23 +01:00
http.createServer(app)
.on("error", function(err){
if (err.code === "EADDRINUSE"){
2014-02-05 15:28:23 +01:00
halt("port " + options.port + " is already is use");
} else {
halt(err.message);
}
})
2013-06-15 00:41:43 +01:00
.listen(options.port);
2013-06-21 18:43:33 +01:00
process.stderr.write("serving at http://localhost:" + options.port + "\n");
2013-06-15 00:41:43 +01:00
}
2014-01-24 19:49:11 +01:00
2014-02-01 23:10:18 +01:00
// open several sites with one command $ ws project1:8000 project2:8100
// store port in package.json, e.g. "ws-port": 9000
// ws -a to open a whole slew of sites stored in json
// ws -o to open in default browser
// ch, ff, op, sa, ca etc. to open in specific browser