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.

104 lines
2.6 KiB

11 years ago
9 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
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 os = require('os')
  11. const arrayify = require('array-back')
  12. const cli = commandLineArgs(cliOptions.definitions)
  13. const usage = cli.getUsage(cliOptions.usageData)
  14. const stored = loadConfig('local-web-server')
  15. const options = collectOptions()
  16. if (options.misc.help) stop(usage, 0)
  17. if (options.misc.config) stop(JSON.stringify(options.server, null, ' '), 0)
  18. const app = localWebServer({
  19. static: {
  20. root: options.server.directory,
  21. options: {
  22. hidden: true
  23. }
  24. },
  25. serveIndex: {
  26. path: options.server.directory,
  27. options: {
  28. icons: true,
  29. hidden: true
  30. }
  31. },
  32. log: {
  33. format: options.server['log-format']
  34. },
  35. compress: options.server.compress,
  36. mime: options.server.mime,
  37. forbid: options.server.forbid,
  38. spa: options.server.spa,
  39. 'no-cache': options.server['no-cache'],
  40. rewrite: options.server.rewrite,
  41. verbose: options.server.verbose,
  42. mocks: options.server.mocks
  43. })
  44. app.listen(options.server.port, onServerUp)
  45. function stop (msgs, exitCode) {
  46. arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
  47. process.exit(exitCode)
  48. }
  49. function onServerUp () {
  50. const ipList = Object.keys(os.networkInterfaces())
  51. .map(key => os.networkInterfaces()[key])
  52. .reduce((prev, curr) => prev = prev.concat(curr), [])
  53. .filter(iface => iface.family === 'IPv4')
  54. .map(iface => `[underline]{${iface.address}:${options.server.port}}`)
  55. .join(', ')
  56. console.error(ansi.format(
  57. path.resolve(options.server.directory) === process.cwd()
  58. ? `serving at ${ipList}`
  59. : `serving [underline]{${options.server.directory}} at ${ipList}`
  60. ))
  61. }
  62. function collectOptions () {
  63. let options = {}
  64. /* parse command line args */
  65. try {
  66. options = cli.parse()
  67. } catch (err) {
  68. stop([ `[red]{Error}: ${err.message}`, usage ], 1)
  69. }
  70. const builtIn = {
  71. port: 8000,
  72. directory: process.cwd(),
  73. forbid: [],
  74. rewrite: []
  75. }
  76. if (options.server.rewrite) {
  77. options.server.rewrite = parseRewriteRules(options.server.rewrite)
  78. }
  79. /* override built-in defaults with stored config and then command line args */
  80. options.server = Object.assign(builtIn, stored, options.server)
  81. return options
  82. }
  83. function parseRewriteRules (rules) {
  84. return rules && rules.map(rule => {
  85. const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
  86. return {
  87. from: matches[1],
  88. to: matches[2]
  89. }
  90. })
  91. }