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.

107 lines
2.5 KiB

11 years ago
9 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env node
  2. 'use strict'
  3. const localWebServer = require('../')
  4. const cliOptions = require('../lib/cli-options')
  5. const commandLineArgs = require('command-line-args')
  6. const ansi = require('ansi-escape-sequences')
  7. const loadConfig = require('config-master')
  8. const path = require('path')
  9. const s = require('string-tools')
  10. const cli = commandLineArgs(cliOptions.definitions)
  11. const usage = cli.getUsage(cliOptions.usageData)
  12. const stored = loadConfig('local-web-server')
  13. const options = collectOptions()
  14. // TODO summary line on server launch
  15. if (options.misc.help) {
  16. console.log(usage)
  17. process.exit(0)
  18. }
  19. if (options.misc.config) {
  20. console.log(JSON.stringify(options.server, null, ' '))
  21. process.exit(0)
  22. }
  23. const app = localWebServer({
  24. static: {
  25. root: options.server.directory,
  26. options: {
  27. hidden: true
  28. }
  29. },
  30. serveIndex: {
  31. path: options.server.directory,
  32. options: {
  33. icons: true,
  34. hidden: true
  35. }
  36. },
  37. log: {
  38. format: options.server['log-format']
  39. },
  40. compress: options.server.compress,
  41. mime: options.server.mime,
  42. forbid: options.server.forbid,
  43. spa: options.server.spa,
  44. 'no-cache': options.server['no-cache'],
  45. rewrite: options.server.rewrite,
  46. verbose: options.server.verbose
  47. })
  48. app
  49. .on('verbose', (category, message) => {
  50. console.error(ansi.format(s.padRight(category, 14), 'bold'), message)
  51. })
  52. .listen(options.server.port, onServerUp)
  53. function halt (err) {
  54. console.log(ansi.format(`Error: ${err.message}`, 'red'))
  55. console.log(usage)
  56. process.exit(1)
  57. }
  58. function onServerUp () {
  59. console.error(ansi.format(
  60. path.resolve(options.server.directory) === process.cwd()
  61. ? `serving at [underline]{http://localhost:${options.server.port}}`
  62. : `serving [underline]{${options.server.directory}} at [underline]{http://localhost:${options.server.port}}`
  63. ))
  64. }
  65. function collectOptions () {
  66. let options = {}
  67. /* parse command line args */
  68. try {
  69. options = cli.parse()
  70. } catch (err) {
  71. halt(err)
  72. }
  73. const builtIn = {
  74. port: 8000,
  75. directory: process.cwd(),
  76. forbid: [],
  77. rewrite: []
  78. }
  79. if (options.server.rewrite) {
  80. options.server.rewrite = parseRewriteRules(options.server.rewrite)
  81. }
  82. /* override built-in defaults with stored config and then command line args */
  83. options.server = Object.assign(builtIn, stored, options.server)
  84. return options
  85. }
  86. function parseRewriteRules (rules) {
  87. return rules && rules.map(rule => {
  88. const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
  89. return {
  90. from: matches[1],
  91. to: matches[2]
  92. }
  93. })
  94. }