From f558855d3bdf3961d67b7819c44e3871ee8d9124 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Wed, 26 Feb 2014 15:10:46 +0100 Subject: [PATCH] store config in package.json or .local-web-server.json --- ws.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/ws.js b/ws.js index fb6bcea..50fce65 100755 --- a/ws.js +++ b/ws.js @@ -4,8 +4,10 @@ require("console-dope"); var connect = require("connect"), http = require("http"), util = require("util"), + fs = require("fs"), Thing = require("nature").Thing, - w = require("wodge"); + w = require("wodge"), + path = require("path"); var usage = "usage: ws [--directory|-d ] [--port|-p ] [--log-format|-f dev|default|short|tiny]"; @@ -15,6 +17,14 @@ function halt(message){ process.exit(1); } +function handleServerError(err){ + if (err.code === "EADDRINUSE"){ + halt("port " + argv.port + " is already is use"); + } else { + halt(err.message); + } +} + /** parse command-line args */ @@ -29,14 +39,22 @@ var argv = new Thing() }) .set(process.argv); -function handleServerError(err){ - if (err.code === "EADDRINUSE"){ - halt("port " + argv.port + " is already is use"); - } else { - halt(err.message); - } +/* +Include any options from "package.json", ".local-web-server.json" or "~/.local-web-server.json", in that order +*/ +var pkgPath = path.join(process.cwd(), "package.json"), + lwsPath = path.join(process.cwd(), ".local-web-server.json"), + homePath = path.join(w.getHomeDir(), ".local-web-server.json"); +if (fs.existsSync(pkgPath)){ + argv.set(require(pkgPath)["local-web-server"]); } - +if (fs.existsSync(lwsPath)){ + argv.set(require(lwsPath)); +} +if (fs.existsSync(homePath)){ + argv.set(require(homePath)); +} + /** Die here if invalid args received */