-
Notifications
You must be signed in to change notification settings - Fork 87
/
cli-view.js
56 lines (51 loc) · 1.41 KB
/
cli-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
class CliView {
constructor (localWebServer) {
this.localWebServer = localWebServer
}
/**
* @example
* { log: 'whatever' }
* { config: { static: { root: 1, hidden: 2 } } }
*/
write (msg) {
const writeToStdout = [ 'log', 'info' ]
Object.keys(msg).forEach(key => {
if (writeToStdout.includes(key)) {
console.log(msg[key])
} else if (key === 'config' && msg.config && this.localWebServer.options.verbose) {
printLine(msg.config)
} else if (key === 'error') {
const ansi = require('ansi-escape-sequences')
console.error(ansi.format(msg.error, 'red'))
}
})
}
}
module.exports = CliView
function printLine (config) {
const output = objectToTable(config)
process.stderr.write(output)
}
/**
* create a nested table for deep object trees
*/
function objectToTable (object) {
const ansi = require('ansi-escape-sequences')
const tableLayout = require('table-layout')
const t = require('typical')
const data = Object.keys(object).map(key => {
if (t.isObject(object[key])) {
return { key: ansi.format(key, 'bold'), value: objectToTable(object[key]) }
} else {
return { key: ansi.format(key, 'bold'), value: object[key] }
}
})
return tableLayout(data, {
padding: { left: '', right: ' ' },
columns: [
// { name: 'key', width: 18 },
// { name: 'value', nowrap: true }
]
})
}