using more-console
This commit is contained in:
@ -10,6 +10,6 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"connect": "~2.12.0",
|
"connect": "~2.12.0",
|
||||||
"nature": "~0.3.0",
|
"nature": "~0.3.0",
|
||||||
"wodge": "~0.1.5"
|
"more-console": "~0.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
62
ws.js
62
ws.js
@ -1,13 +1,23 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
"use strict";
|
||||||
|
require("more-console");
|
||||||
var connect = require("connect"),
|
var connect = require("connect"),
|
||||||
http = require("http"),
|
http = require("http"),
|
||||||
util = require("util"),
|
util = require("util"),
|
||||||
Thing = require("nature").Thing,
|
Thing = require("nature").Thing;
|
||||||
wodge = require("wodge");
|
|
||||||
|
|
||||||
var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
|
var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
|
||||||
|
|
||||||
var options = new Thing()
|
function halt(message){
|
||||||
|
console.red.log("Error: %s", message);
|
||||||
|
console.log(usage);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
parse command-line args
|
||||||
|
*/
|
||||||
|
var argv = new Thing()
|
||||||
.define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
|
.define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
|
||||||
.define({ name: "log-format", alias: "f", type: "string", value: "dev" })
|
.define({ name: "log-format", alias: "f", type: "string", value: "dev" })
|
||||||
.define({ name: "help", alias: "h", type: "boolean" })
|
.define({ name: "help", alias: "h", type: "boolean" })
|
||||||
@ -20,22 +30,22 @@ var options = new Thing()
|
|||||||
|
|
||||||
function handleServerError(err){
|
function handleServerError(err){
|
||||||
if (err.code === "EADDRINUSE"){
|
if (err.code === "EADDRINUSE"){
|
||||||
halt("port " + options.port + " is already is use");
|
halt("port " + argv.port + " is already is use");
|
||||||
} else {
|
} else {
|
||||||
halt(err.message);
|
halt(err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function halt(message){
|
/**
|
||||||
console.log(wodge.red("Error ") + message);
|
Die here if invalid args received
|
||||||
console.log(usage);
|
*/
|
||||||
process.exit(1);
|
if (!argv.valid) halt(argv.validationMessages);
|
||||||
}
|
|
||||||
|
|
||||||
if (!options.valid){
|
|
||||||
halt(options.validationMessages);
|
|
||||||
|
|
||||||
} else if (options.help){
|
/**
|
||||||
|
$ ws --help
|
||||||
|
*/
|
||||||
|
if (argv.help){
|
||||||
console.log(usage);
|
console.log(usage);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -50,35 +60,33 @@ if (!options.valid){
|
|||||||
|
|
||||||
var app = connect();
|
var app = connect();
|
||||||
|
|
||||||
if(options.stats){
|
if(argv.stats){
|
||||||
var reqCount = 0;
|
var reqCount = 0;
|
||||||
app.use(function(req, res, next){
|
app.use(function(req, res, next){
|
||||||
if (reqCount === 0){
|
if (reqCount === 0){
|
||||||
process.stdout.write("Files served: ");
|
console.write("Files served: ");
|
||||||
}
|
}
|
||||||
process.stdout.write(reqCount.toString());
|
console.column(15).write(reqCount++);
|
||||||
reqCount++;
|
|
||||||
process.stdout.write("\x1b[15G");
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
app.use(connect.logger(options["log-format"]));
|
app.use(connect.logger(argv["log-format"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use(connect.compress())
|
app.use(connect.compress())
|
||||||
.use(connect.static(options.directory))
|
.use(connect.static(argv.directory))
|
||||||
.use(connect.directory(options.directory, { icons: true }));
|
.use(connect.directory(argv.directory, { icons: true }));
|
||||||
|
|
||||||
http.createServer(app)
|
http.createServer(app)
|
||||||
.on("error", handleServerError)
|
.on("error", handleServerError)
|
||||||
.listen(options.port);
|
.listen(argv.port);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
write to stderr so stdout can be piped to disk ($ ws > log.txt)
|
write status to stderr so stdout can be piped to disk ($ ws > log.txt)
|
||||||
*/
|
*/
|
||||||
console.error(util.format(
|
if (argv.directory === process.cwd()){
|
||||||
"serving %sat %s",
|
console.error("serving at %u{%s}", "http://localhost:" + argv.port);
|
||||||
options.directory === process.cwd() ? "" : wodge.underline(options.directory) + " ",
|
} else {
|
||||||
wodge.underline("http://localhost:" + options.port)
|
console.error("serving %u{%s} at %u{%s}", argv.directory, "http://localhost:" + argv.port);
|
||||||
));
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user