You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.0 KiB

12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. #!/usr/bin/env node
  2. var connect = require("connect"),
  3. http = require("http"),
  4. Thing = require("nature").Thing,
  5. wodge = require("wodge");
  6. function halt(message){
  7. console.log(wodge.red("Error ") + message);
  8. console.log(usage);
  9. process.exit(1);
  10. }
  11. var usage = "usage: ws [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
  12. var options = new Thing()
  13. .define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
  14. .define({ name: "log-format", alias: "f", type: "string", value: "dev" })
  15. .define({ name: "help", alias: "h", type: "boolean" })
  16. .on("error", function(err){
  17. halt(err.message);
  18. })
  19. .set(process.argv);
  20. if (!options.valid){
  21. halt(options.validationMessages)
  22. } else if (options.help){
  23. console.log(usage);
  24. } else {
  25. /**
  26. customised connect.logger :date token, purely to satisfy Logstalgia.
  27. */
  28. connect.logger.token('date', function(req, res){
  29. var a = new Date();
  30. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  31. .replace("GMT", "").replace(" (BST)", "");
  32. });
  33. var app = connect()
  34. .use(connect.logger(options["log-format"]))
  35. .use(connect.compress())
  36. .use(connect.static(process.cwd()))
  37. .use(connect.directory(process.cwd(), { icons: true }));
  38. http.createServer(app)
  39. .on("error", function(err){
  40. if (err.code === "EADDRINUSE"){
  41. halt("port " + options.port + " is already is use")
  42. } else {
  43. halt(err.message);
  44. }
  45. })
  46. .listen(options.port);
  47. process.stderr.write("serving at http://localhost:" + options.port + "\n");
  48. }
  49. // open several sites with one command $ ws project1:8000 project2:8100
  50. // store port in package.json, e.g. "ws-port": 9000
  51. // ws -a to open a whole slew of sites stored in json
  52. // ws -o to open in default browser
  53. // ch, ff, op, sa, ca etc. to open in specific browser