|
@ -5,7 +5,6 @@ var dope = require("console-dope"), |
|
|
http = require("http"), |
|
|
http = require("http"), |
|
|
cliArgs = require("command-line-args"), |
|
|
cliArgs = require("command-line-args"), |
|
|
o = require("object-tools"), |
|
|
o = require("object-tools"), |
|
|
s = require("string-tools"), |
|
|
|
|
|
path = require("path"), |
|
|
path = require("path"), |
|
|
loadConfig = require("config-master"), |
|
|
loadConfig = require("config-master"), |
|
|
morgan = require("morgan"), |
|
|
morgan = require("morgan"), |
|
@ -13,7 +12,6 @@ var dope = require("console-dope"), |
|
|
directory = require("serve-index"), |
|
|
directory = require("serve-index"), |
|
|
compress = require("compression"), |
|
|
compress = require("compression"), |
|
|
homePath = require("home-path"), |
|
|
homePath = require("home-path"), |
|
|
byteSize = require("byte-size"), |
|
|
|
|
|
logStats = require("stream-log-stats"); |
|
|
logStats = require("stream-log-stats"); |
|
|
|
|
|
|
|
|
var usage = |
|
|
var usage = |
|
@ -28,10 +26,10 @@ function halt(message){ |
|
|
process.exit(1); |
|
|
process.exit(1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* Merge together options from |
|
|
|
|
|
|
|
|
/* Load and merge together options from |
|
|
- ~/.local-web-server.json |
|
|
- ~/.local-web-server.json |
|
|
- {cwd}/.local-web-server.json |
|
|
- {cwd}/.local-web-server.json |
|
|
- {cwd}/package.json |
|
|
|
|
|
|
|
|
- the `local-web-server` property of {cwd}/package.json |
|
|
*/ |
|
|
*/ |
|
|
var storedConfig = loadConfig( |
|
|
var storedConfig = loadConfig( |
|
|
path.join(homePath(), ".local-web-server.json"), |
|
|
path.join(homePath(), ".local-web-server.json"), |
|
@ -39,20 +37,27 @@ var storedConfig = loadConfig( |
|
|
{ jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" } |
|
|
{ jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" } |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
/* override stored config with values parsed from command line */ |
|
|
|
|
|
|
|
|
/* parse command line args */ |
|
|
try { |
|
|
try { |
|
|
var argv = cliArgs([ |
|
|
var argv = cliArgs([ |
|
|
{ name: "port", alias: "p", type: Number, defaultOption: true }, |
|
|
{ name: "port", alias: "p", type: Number, defaultOption: true }, |
|
|
{ name: "log-format", alias: "f", type: String }, |
|
|
{ name: "log-format", alias: "f", type: String }, |
|
|
{ name: "help", alias: "h", type: Boolean }, |
|
|
{ name: "help", alias: "h", type: Boolean }, |
|
|
{ name: "directory", alias: "d", type: String, value: process.cwd() }, |
|
|
|
|
|
|
|
|
{ name: "directory", alias: "d", type: String }, |
|
|
{ name: "config", type: Boolean }, |
|
|
{ name: "config", type: Boolean }, |
|
|
{ name: "compress", alias: "c", type: Boolean } |
|
|
|
|
|
|
|
|
{ name: "compress", alias: "c", type: Boolean }, |
|
|
|
|
|
{ name: "refreshRate", alias: "r", type: Number } |
|
|
]).parse(); |
|
|
]).parse(); |
|
|
} catch(err){ |
|
|
} catch(err){ |
|
|
halt(err.message); |
|
|
halt(err.message); |
|
|
} |
|
|
} |
|
|
argv = o.extend({ port: 8000 }, storedConfig, argv); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* override built-in defaults with stored config then command line args */ |
|
|
|
|
|
argv = o.extend({ |
|
|
|
|
|
port: 8000, |
|
|
|
|
|
directory: process.cwd(), |
|
|
|
|
|
refreshRate: 500 |
|
|
|
|
|
}, storedConfig, argv); |
|
|
|
|
|
|
|
|
if (argv.config){ |
|
|
if (argv.config){ |
|
|
dope.log("Stored config: "); |
|
|
dope.log("Stored config: "); |
|
@ -63,12 +68,6 @@ if (argv.config){ |
|
|
dope.log(usage); |
|
|
dope.log(usage); |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
var total = { |
|
|
|
|
|
req: 0, |
|
|
|
|
|
bytes: 0, |
|
|
|
|
|
connections: 0 |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
process.on("SIGINT", function(){ |
|
|
process.on("SIGINT", function(){ |
|
|
dope.showCursor(); |
|
|
dope.showCursor(); |
|
|
dope.log(); |
|
|
dope.log(); |
|
@ -94,7 +93,7 @@ if (argv.config){ |
|
|
/* if no specific `--log-format` required, pipe the default web log output |
|
|
/* if no specific `--log-format` required, pipe the default web log output |
|
|
into `log-stats`, which prints statistics to the console */ |
|
|
into `log-stats`, which prints statistics to the console */ |
|
|
} else { |
|
|
} else { |
|
|
app.use(morgan({ stream: logStats() })); |
|
|
|
|
|
|
|
|
app.use(morgan({ stream: logStats({ refreshRate: argv.refreshRate }) })); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* --compress enables compression */ |
|
|
/* --compress enables compression */ |
|
@ -105,7 +104,7 @@ if (argv.config){ |
|
|
.use(directory(path.resolve(argv.directory), { icons: true })); |
|
|
.use(directory(path.resolve(argv.directory), { icons: true })); |
|
|
|
|
|
|
|
|
/* launch server */ |
|
|
/* launch server */ |
|
|
var server = http.createServer(app) |
|
|
|
|
|
|
|
|
http.createServer(app) |
|
|
.on("error", function(err){ |
|
|
.on("error", function(err){ |
|
|
if (err.code === "EADDRINUSE"){ |
|
|
if (err.code === "EADDRINUSE"){ |
|
|
halt("port " + argv.port + " is already is use"); |
|
|
halt("port " + argv.port + " is already is use"); |
|
|