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.

48 lines
1.3 KiB

  1. 'use strict'
  2. class CliView {
  3. constructor (localWebServer) {
  4. this.localWebServer = localWebServer
  5. }
  6. write (msg) {
  7. const writeToStdout = [ 'log', 'info' ]
  8. Object.keys(msg).forEach(key => {
  9. if (writeToStdout.includes(key)) {
  10. console.log(msg[key])
  11. } else if (key === 'config' && msg.config && this.localWebServer.options.verbose) {
  12. printLine(msg.config)
  13. } else if (key === 'error') {
  14. const ansi = require('ansi-escape-sequences')
  15. console.error(ansi.format(msg.error, 'red'))
  16. }
  17. })
  18. }
  19. }
  20. module.exports = CliView
  21. function printLine (config) {
  22. const output = objectToTable(config)
  23. process.stderr.write(output)
  24. }
  25. function objectToTable (object) {
  26. const ansi = require('ansi-escape-sequences')
  27. const tableLayout = require('table-layout')
  28. const t = require('typical')
  29. const data = Object.keys(object).map(key => {
  30. if (t.isObject(object[key])) {
  31. return { key: ansi.format(key, 'bold'), value: objectToTable(object[key]) }
  32. } else {
  33. return { key: ansi.format(key, 'bold'), value: object[key] }
  34. }
  35. })
  36. return tableLayout(data, {
  37. padding: { left: '', right: ' ' },
  38. columns: [
  39. // { name: 'key', width: 18 },
  40. // { name: 'value', nowrap: true }
  41. ]
  42. })
  43. }