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.

133 lines
3.4 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 s = require('string-tools')
  10. const os = require('os')
  11. const arrayify = require('array-back')
  12. const t = require('typical')
  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. let isHttps = false
  47. if (options.server.key && options.server.cert) {
  48. const https = require('https')
  49. const fs = require('fs')
  50. isHttps = true
  51. const serverOptions = {
  52. key: fs.readFileSync(options.server.key),
  53. cert: fs.readFileSync(options.server.cert)
  54. }
  55. const server = https.createServer(serverOptions, app.callback())
  56. server.listen(options.server.port, onServerUp)
  57. } else {
  58. app.listen(options.server.port, onServerUp)
  59. }
  60. function stop (msgs, exitCode) {
  61. arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
  62. process.exit(exitCode)
  63. }
  64. function onServerUp () {
  65. let ipList = Object.keys(os.networkInterfaces())
  66. .map(key => os.networkInterfaces()[key])
  67. .reduce((prev, curr) => prev = prev.concat(curr), [])
  68. .filter(iface => iface.family === 'IPv4')
  69. ipList.unshift({ address: os.hostname() })
  70. ipList = ipList
  71. .map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.server.port}}`)
  72. .join(', ')
  73. console.error(ansi.format(
  74. path.resolve(options.server.directory) === process.cwd()
  75. ? `serving at ${ipList}`
  76. : `serving [underline]{${options.server.directory}} at ${ipList}`
  77. ))
  78. }
  79. function collectOptions () {
  80. let options = {}
  81. /* parse command line args */
  82. try {
  83. options = cli.parse()
  84. } catch (err) {
  85. stop([ `[red]{Error}: ${err.message}`, usage ], 1)
  86. }
  87. const builtIn = {
  88. port: 8000,
  89. directory: process.cwd(),
  90. forbid: [],
  91. rewrite: []
  92. }
  93. if (options.server.rewrite) {
  94. options.server.rewrite = parseRewriteRules(options.server.rewrite)
  95. }
  96. /* override built-in defaults with stored config and then command line args */
  97. options.server = Object.assign(builtIn, stored, options.server)
  98. return options
  99. }
  100. function parseRewriteRules (rules) {
  101. return rules && rules.map(rule => {
  102. const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
  103. return {
  104. from: matches[1],
  105. to: matches[2]
  106. }
  107. })
  108. }
  109. function validateOptions (options) {
  110. function invalid (msg) {
  111. return `[red underline]{Invalid:} [bold]{${msg}}`
  112. }
  113. if (!t.isNumber(options.server.port)) {
  114. stop([ invalid(`--port must be numeric [value=${options.server.port}]`), usage ], 1)
  115. }
  116. }