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.6 KiB

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
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 ansi = require('ansi-escape-sequences')
  4. const path = require('path')
  5. const arrayify = require('array-back')
  6. const t = require('typical')
  7. const CommandLineTool = require('command-line-tool')
  8. const MiddlewareStack = require('./middleware-stack')
  9. const debug = require('./debug')
  10. const tool = new CommandLineTool()
  11. class LocalWebServer extends MiddlewareStack {
  12. getApplication () {
  13. const Koa = require('koa')
  14. const app = new Koa()
  15. app.use(this.compose(this.options))
  16. return app
  17. }
  18. getServer () {
  19. const options = this.options
  20. let key = options.key
  21. let cert = options.cert
  22. const app = this.getApplication()
  23. app.on('error', err => {
  24. if (options['log-format']) {
  25. console.error(ansi.format(err.message, 'red'))
  26. }
  27. })
  28. if (options.https) {
  29. key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
  30. cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
  31. }
  32. let server = null
  33. if (key && cert) {
  34. const fs = require('fs')
  35. const serverOptions = {
  36. key: fs.readFileSync(key),
  37. cert: fs.readFileSync(cert)
  38. }
  39. const https = require('https')
  40. server = https.createServer(serverOptions, app.callback())
  41. server.isHttps = true
  42. } else {
  43. const http = require('http')
  44. server = http.createServer(app.callback())
  45. }
  46. return server
  47. }
  48. start () {
  49. const options = collectOptions(this.getOptionDefinitions())
  50. this.options = options
  51. if (options.verbose) {
  52. debug.setLevel(1)
  53. }
  54. if (options.config) {
  55. tool.stop(JSON.stringify(options, null, ' '), 0)
  56. } else if (options.version) {
  57. const pkg = require(path.resolve(__dirname, '..', 'package.json'))
  58. tool.stop(pkg.version)
  59. } else {
  60. const server = this.getServer()
  61. server.listen(options.port, onServerUp.bind(null, options, server.isHttps))
  62. return server
  63. }
  64. }
  65. }
  66. function onServerUp (options, isHttps) {
  67. const ipList = getIPList(isHttps)
  68. .map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.port}}`)
  69. .join(', ')
  70. console.error(ansi.format(
  71. path.resolve(options.directory) === process.cwd()
  72. ? `serving at ${ipList}`
  73. : `serving [underline]{${options.directory}} at ${ipList}`
  74. ))
  75. }
  76. function getIPList (isHttps) {
  77. const flatten = require('reduce-flatten')
  78. const os = require('os')
  79. let ipList = Object.keys(os.networkInterfaces())
  80. .map(key => os.networkInterfaces()[key])
  81. .reduce(flatten, [])
  82. .filter(iface => iface.family === 'IPv4')
  83. ipList.unshift({ address: os.hostname() })
  84. return ipList
  85. }
  86. /**
  87. * Return default, stored and command-line options combined
  88. */
  89. function collectOptions (mwOptionDefinitions) {
  90. const loadConfig = require('config-master')
  91. const stored = loadConfig('local-web-server')
  92. const cli = require('../lib/cli-data')
  93. /* parse command line args */
  94. const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions))
  95. let cliOptions = tool.getOptions(definitions, cli.usage(definitions))
  96. /* override built-in defaults with stored config and then command line options */
  97. const options = Object.assign({
  98. port: 8000,
  99. directory: process.cwd()
  100. }, stored, cliOptions.server, cliOptions.middleware, cliOptions.misc)
  101. // console.error(require('util').inspect(options, { depth: 3, colors: true }))
  102. validateOptions(options)
  103. return options
  104. }
  105. function validateOptions (options) {
  106. if (!t.isNumber(options.port)) {
  107. tool.printError('--port must be numeric')
  108. console.error(tool.usage)
  109. tool.halt()
  110. }
  111. }
  112. module.exports = LocalWebServer