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.

68 lines
2.1 KiB

11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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. util = require("util"),
  5. Thing = require("nature").Thing,
  6. wodge = require("wodge");
  7. var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
  8. var options = new Thing()
  9. .define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
  10. .define({ name: "log-format", alias: "f", type: "string", value: "dev" })
  11. .define({ name: "help", alias: "h", type: "boolean" })
  12. .define({ name: "directory", alias: "d", type: "string", value: process.cwd() })
  13. .on("error", function(err){
  14. halt(err.message);
  15. })
  16. .set(process.argv);
  17. function handleServerError(err){
  18. if (err.code === "EADDRINUSE"){
  19. halt("port " + options.port + " is already is use");
  20. } else {
  21. halt(err.message);
  22. }
  23. }
  24. function halt(message){
  25. console.log(wodge.red("Error ") + message);
  26. console.log(usage);
  27. process.exit(1);
  28. }
  29. if (!options.valid){
  30. halt(options.validationMessages);
  31. } else if (options.help){
  32. console.log(usage);
  33. } else {
  34. /**
  35. customised connect.logger :date token, purely to satisfy Logstalgia.
  36. */
  37. connect.logger.token("date", function(){
  38. var a = new Date();
  39. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  40. .replace("GMT", "").replace(" (BST)", "");
  41. });
  42. var app = connect()
  43. .use(connect.logger(options["log-format"]))
  44. .use(connect.compress())
  45. .use(connect.static(options.directory))
  46. .use(connect.directory(options.directory, { icons: true }));
  47. http.createServer(app)
  48. .on("error", handleServerError)
  49. .listen(options.port);
  50. /*
  51. write to stderr so not to appear in logs piped to disk ($ ws > log.txt)
  52. */
  53. console.error(util.format(
  54. "serving %sat %s",
  55. options.directory === process.cwd() ? "" : wodge.underline(options.directory) + " ",
  56. wodge.underline("http://localhost:" + options.port)
  57. ));
  58. }