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.

36 lines
1015 B

10 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env node
  2. 'use strict'
  3. const path = require('path')
  4. function onServerUp (port, directory, isHttps) {
  5. const ansi = require('ansi-escape-sequences')
  6. const ipList = getIPList()
  7. .map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${port}}`)
  8. .join(', ')
  9. console.error(ansi.format(
  10. path.resolve(directory || '') === process.cwd()
  11. ? `serving at ${ipList}`
  12. : `serving [underline]{${directory}} at ${ipList}`
  13. ))
  14. }
  15. function getIPList () {
  16. const flatten = require('reduce-flatten')
  17. const os = require('os')
  18. let ipList = Object.keys(os.networkInterfaces())
  19. .map(key => os.networkInterfaces()[key])
  20. .reduce(flatten, [])
  21. .filter(iface => iface.family === 'IPv4')
  22. ipList.unshift({ address: os.hostname() })
  23. return ipList
  24. }
  25. const LocalWebServer = require('../')
  26. const ws = new LocalWebServer()
  27. const server = ws.getServer()
  28. server.on('listening', function () {
  29. onServerUp(ws.options.port, ws.options['static.root'], server.isHttps)
  30. })