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.

126 lines
3.8 KiB

11 years ago
11 years ago
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
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. var dope = require("console-dope"),
  4. http = require("http"),
  5. cliArgs = require("command-line-args"),
  6. o = require("object-tools"),
  7. path = require("path"),
  8. loadConfig = require("config-master"),
  9. homePath = require("home-path"),
  10. logStats = require("stream-log-stats"),
  11. connect = require("connect"),
  12. morgan = require("morgan"),
  13. serveStatic = require("serve-static"),
  14. directory = require("serve-index"),
  15. compress = require("compression"),
  16. cliOptions = require("../lib/cli-options");
  17. var cli = cliArgs(cliOptions);
  18. var usage = cli.usage({
  19. forms: [
  20. "$ ws <server options>",
  21. "$ ws --config",
  22. "$ ws --help"
  23. ]
  24. });
  25. /* parse command line args */
  26. try {
  27. var argv = cli.parse();
  28. } catch(err){
  29. halt(err.message);
  30. }
  31. /* Load and merge together options from
  32. - ~/.local-web-server.json
  33. - {cwd}/.local-web-server.json
  34. - the `local-web-server` property of {cwd}/package.json
  35. */
  36. var storedConfig = loadConfig(
  37. path.join(homePath(), ".local-web-server.json"),
  38. path.join(process.cwd(), ".local-web-server.json"),
  39. { jsonPath: path.join(process.cwd(), "package.json"), configProperty: "local-web-server" }
  40. );
  41. var builtInDefaults = {
  42. port: 8000,
  43. directory: process.cwd(),
  44. refreshRate: 500
  45. };
  46. /* override built-in defaults with stored config and then command line args */
  47. argv.Server = o.extend(builtInDefaults, storedConfig, argv.Server);
  48. if (argv.Misc.config){
  49. dope.log("Stored config: ");
  50. dope.log(storedConfig);
  51. process.exit(0);
  52. } else if (argv.Misc.help){
  53. dope.log(usage);
  54. } else {
  55. process.on("SIGINT", function(){
  56. dope.showCursor();
  57. dope.log();
  58. process.exit(0);
  59. });
  60. var app = connect();
  61. /* log using --log-format (if supplied) */
  62. var logFormat = argv.Server["log-format"];
  63. if(logFormat) {
  64. if (logFormat === "none"){
  65. // do nothing, no logging required
  66. } else {
  67. if (logFormat === "logstalgia"){
  68. /* customised logger :date token, purely to satisfy Logstalgia. */
  69. morgan.token("date", function(){
  70. var a = new Date();
  71. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  72. .replace("GMT", "").replace(" (BST)", "");
  73. });
  74. logFormat = "default";
  75. }
  76. app.use(morgan(logFormat));
  77. }
  78. /* if no `--log-format` was specified, pipe the default format output
  79. into `log-stats`, which prints statistics to the console */
  80. } else {
  81. app.use(morgan({ stream: logStats({ refreshRate: argv.Server.refreshRate }) }));
  82. }
  83. /* --compress enables compression */
  84. if (argv.Server.compress) app.use(compress());
  85. /* static file server including directory browsing support */
  86. app.use(serveStatic(path.resolve(argv.Server.directory)))
  87. .use(directory(path.resolve(argv.Server.directory), { icons: true }));
  88. /* launch server */
  89. http.createServer(app)
  90. .on("error", function(err){
  91. if (err.code === "EADDRINUSE"){
  92. halt("port " + argv.Server.port + " is already is use");
  93. } else {
  94. halt(err.message);
  95. }
  96. })
  97. .listen(argv.Server.port);
  98. /* write launch information to stderr (stdout is reserved for web log output) */
  99. if (path.resolve(argv.Server.directory) === process.cwd()){
  100. dope.error("serving at %underline{%s}", "http://localhost:" + argv.Server.port);
  101. } else {
  102. dope.error("serving %underline{%s} at %underline{%s}", argv.Server.directory, "http://localhost:" + argv.Server.port);
  103. }
  104. }
  105. function halt(message){
  106. dope.red.log("Error: %s", message);
  107. dope.log(usage);
  108. process.exit(1);
  109. }