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.

57 lines
1.8 KiB

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