#!/usr/bin/env node var connect = require("connect"), http = require("http"), Thing = require("nature").Thing, wodge = require("wodge"); var usage = "usage: ws [--port|-p ] [--log-format|-f dev|default|short|tiny]"; function halt(message){ console.log(wodge.red("Error ") + message); console.log(usage); process.exit(1); } var options = new Thing() .define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 }) .define({ name: "log-format", alias: "f", type: "string", value: "dev" }) .define({ name: "help", alias: "h", type: "boolean" }) .on("error", function(err){ halt(err.message); }) .set(process.argv); if (!options.valid){ halt(options.validationMessages); } else if (options.help){ console.log(usage); } else { /** customised connect.logger :date token, purely to satisfy Logstalgia. */ connect.logger.token("date", function(){ var a = new Date(); return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString()) .replace("GMT", "").replace(" (BST)", ""); }); var app = connect() .use(connect.logger(options["log-format"])) .use(connect.compress()) .use(connect.static(process.cwd())) .use(connect.directory(process.cwd(), { icons: true })); http.createServer(app) .on("error", function(err){ if (err.code === "EADDRINUSE"){ halt("port " + options.port + " is already is use"); } else { halt(err.message); } }) .listen(options.port); process.stderr.write("serving at http://localhost:" + options.port + "\n"); } // 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