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.

92 lines
2.5 KiB

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
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. "use strict";
  3. require("more-console");
  4. var connect = require("connect"),
  5. http = require("http"),
  6. util = require("util"),
  7. Thing = require("nature").Thing;
  8. var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
  9. function halt(message){
  10. console.red.log("Error: %s", message);
  11. console.log(usage);
  12. process.exit(1);
  13. }
  14. /**
  15. parse command-line args
  16. */
  17. var argv = new Thing()
  18. .define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
  19. .define({ name: "log-format", alias: "f", type: "string", value: "dev" })
  20. .define({ name: "help", alias: "h", type: "boolean" })
  21. .define({ name: "directory", alias: "d", type: "string", value: process.cwd() })
  22. .define({ name: "stats", alias: "s", type: "boolean" })
  23. .on("error", function(err){
  24. halt(err.message);
  25. })
  26. .set(process.argv);
  27. function handleServerError(err){
  28. if (err.code === "EADDRINUSE"){
  29. halt("port " + argv.port + " is already is use");
  30. } else {
  31. halt(err.message);
  32. }
  33. }
  34. /**
  35. Die here if invalid args received
  36. */
  37. if (!argv.valid) halt(argv.validationMessages);
  38. /**
  39. $ ws --help
  40. */
  41. if (argv.help){
  42. console.log(usage);
  43. } else {
  44. /**
  45. customised connect.logger :date token, purely to satisfy Logstalgia.
  46. */
  47. connect.logger.token("date", function(){
  48. var a = new Date();
  49. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  50. .replace("GMT", "").replace(" (BST)", "");
  51. });
  52. var app = connect();
  53. if(argv.stats){
  54. var reqCount = 0;
  55. app.use(function(req, res, next){
  56. if (reqCount === 0){
  57. console.write("Files served: ");
  58. }
  59. console.column(15).write(reqCount++);
  60. next();
  61. });
  62. } else {
  63. app.use(connect.logger(argv["log-format"]));
  64. }
  65. app.use(connect.compress())
  66. .use(connect.static(argv.directory))
  67. .use(connect.directory(argv.directory, { icons: true }));
  68. http.createServer(app)
  69. .on("error", handleServerError)
  70. .listen(argv.port);
  71. /*
  72. write status to stderr so stdout can be piped to disk ($ ws > log.txt)
  73. */
  74. if (argv.directory === process.cwd()){
  75. console.error("serving at %u{%s}", "http://localhost:" + argv.port);
  76. } else {
  77. console.error("serving %u{%s} at %u{%s}", argv.directory, "http://localhost:" + argv.port);
  78. }
  79. }