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.

58 lines
1.8 KiB

11 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
  1. #!/usr/bin/env node
  2. 'use strict'
  3. const localWebServer = require('../')
  4. const commandLineArgs = require('command-line-args')
  5. const ansi = require('ansi-escape-sequences')
  6. const cliOptions = require('../lib/cli-options')
  7. const loadConfig = require('config-master')
  8. const path = require('path')
  9. const options = {}
  10. /* parse command line args */
  11. const cli = commandLineArgs(cliOptions.definitions)
  12. const usage = cli.getUsage(cliOptions.usageData)
  13. try {
  14. options.cli = cli.parse()
  15. } catch (err) {
  16. halt(err.message)
  17. }
  18. options.stored = Object.assign({
  19. blacklist: []
  20. }, loadConfig('local-web-server'))
  21. options.builtIn = {
  22. port: 8000,
  23. directory: process.cwd()
  24. }
  25. /* override built-in defaults with stored config and then command line args */
  26. options.cli.server = Object.assign(options.builtIn, options.stored, options.cli.server)
  27. if (options.cli.misc.help) return console.log(usage)
  28. if (options.cli.misc.config) return console.log(JSON.stringify(options.stored, null, ' '))
  29. localWebServer({
  30. static: { root: options.cli.server.directory },
  31. serveIndex: { path: options.cli.server.directory, options: { icons: true } },
  32. log: { format: options.cli.server['log-format'] },
  33. compress: options.cli.server.compress,
  34. mime: options.stored.mime,
  35. blacklist: options.stored.blacklist.map(regexp => RegExp(regexp, "i"))
  36. }).listen(options.cli.server.port, onServerUp)
  37. function halt (message) {
  38. console.log(ansi.format(`Error: ${message}`, 'red'))
  39. console.log(usage)
  40. process.exit(1)
  41. }
  42. function onServerUp () {
  43. console.error(ansi.format(
  44. path.resolve(options.cli.server.directory) === process.cwd()
  45. ? `serving at [underline]{http://localhost:${options.cli.server.port}}`
  46. : `serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}}`
  47. ))
  48. }