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.

138 lines
3.6 KiB

11 years ago
9 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
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 os = require('os')
  10. const arrayify = require('array-back')
  11. const t = require('typical')
  12. const flatten = require('reduce-flatten')
  13. const cli = commandLineArgs(cliOptions.definitions)
  14. const usage = cli.getUsage(cliOptions.usageData)
  15. const stored = loadConfig('local-web-server')
  16. const options = collectOptions()
  17. if (options.misc.help) stop(usage, 0)
  18. if (options.misc.config) stop(JSON.stringify(options.server, null, ' '), 0)
  19. validateOptions(options)
  20. const app = localWebServer({
  21. static: {
  22. root: options.server.directory,
  23. options: {
  24. hidden: true
  25. }
  26. },
  27. serveIndex: {
  28. path: options.server.directory,
  29. options: {
  30. icons: true,
  31. hidden: true
  32. }
  33. },
  34. log: {
  35. format: options.server['log-format']
  36. },
  37. compress: options.server.compress,
  38. mime: options.server.mime,
  39. forbid: options.server.forbid,
  40. spa: options.server.spa,
  41. 'no-cache': options.server['no-cache'],
  42. rewrite: options.server.rewrite,
  43. verbose: options.server.verbose,
  44. mocks: options.server.mocks
  45. })
  46. if (options.server.https) {
  47. options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
  48. options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
  49. }
  50. let isHttps = false
  51. if (options.server.key && options.server.cert) {
  52. const https = require('https')
  53. const fs = require('fs')
  54. isHttps = true
  55. const serverOptions = {
  56. key: fs.readFileSync(options.server.key),
  57. cert: fs.readFileSync(options.server.cert)
  58. }
  59. const server = https.createServer(serverOptions, app.callback())
  60. server.listen(options.server.port, onServerUp)
  61. } else {
  62. app.listen(options.server.port, onServerUp)
  63. }
  64. function stop (msgs, exitCode) {
  65. arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
  66. process.exit(exitCode)
  67. }
  68. function onServerUp () {
  69. let ipList = Object.keys(os.networkInterfaces())
  70. .map(key => os.networkInterfaces()[key])
  71. .reduce(flatten, [])
  72. .filter(iface => iface.family === 'IPv4')
  73. ipList.unshift({ address: os.hostname() })
  74. ipList = ipList
  75. .map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.server.port}}`)
  76. .join(', ')
  77. console.error(ansi.format(
  78. path.resolve(options.server.directory) === process.cwd()
  79. ? `serving at ${ipList}`
  80. : `serving [underline]{${options.server.directory}} at ${ipList}`
  81. ))
  82. }
  83. function collectOptions () {
  84. let options = {}
  85. /* parse command line args */
  86. try {
  87. options = cli.parse()
  88. } catch (err) {
  89. stop([ `[red]{Error}: ${err.message}`, usage ], 1)
  90. }
  91. const builtIn = {
  92. port: 8000,
  93. directory: process.cwd(),
  94. forbid: [],
  95. rewrite: []
  96. }
  97. if (options.server.rewrite) {
  98. options.server.rewrite = parseRewriteRules(options.server.rewrite)
  99. }
  100. /* override built-in defaults with stored config and then command line args */
  101. options.server = Object.assign(builtIn, stored, options.server)
  102. return options
  103. }
  104. function parseRewriteRules (rules) {
  105. return rules && rules.map(rule => {
  106. const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
  107. return {
  108. from: matches[1],
  109. to: matches[2]
  110. }
  111. })
  112. }
  113. function validateOptions (options) {
  114. function invalid (msg) {
  115. return `[red underline]{Invalid:} [bold]{${msg}}`
  116. }
  117. if (!t.isNumber(options.server.port)) {
  118. stop([ invalid(`--port must be numeric [value=${options.server.port}]`), usage ], 1)
  119. }
  120. }