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.

132 lines
3.4 KiB

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