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.

54 lines
1.7 KiB

11 years ago
9 years ago
11 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 = loadConfig('local-web-server')
  19. options.builtIn = {
  20. port: 8000,
  21. directory: process.cwd()
  22. }
  23. /* override built-in defaults with stored config and then command line args */
  24. options.cli.server = Object.assign(options.builtIn, options.stored, options.cli.server)
  25. if (options.cli.misc.help) return console.log(usage)
  26. if (options.cli.misc.config) return console.log(JSON.stringify(options.stored, null, ' '))
  27. localWebServer({
  28. static: { root: options.cli.server.directory },
  29. serveIndex: { path: options.cli.server.directory, options: { icons: true } },
  30. log: { format: options.cli.server['log-format'] },
  31. compress: options.cli.server.compress,
  32. mime: options.stored.mime
  33. }).listen(options.cli.server.port, onServerUp)
  34. function halt (message) {
  35. console.log(ansi.format(`Error: ${message}`, 'red'))
  36. console.log(usage)
  37. process.exit(1)
  38. }
  39. function onServerUp () {
  40. console.error(ansi.format(
  41. path.resolve(options.cli.server.directory) === process.cwd()
  42. ? `serving at [underline]{http://localhost:${options.cli.server.port}}`
  43. : `serving [underline]{${options.cli.server.directory}} at [underline]{http://localhost:${options.cli.server.port}}`
  44. ))
  45. }