UNPKG

3.85 kBJavaScriptView Raw
1'use strict'
2
3let cli = require('..')
4let util = require('util')
5
6/**
7 * styledJSON prints out colored, indented JSON
8 *
9 * @example
10 * styledHeader({foo: 'bar'}) # Outputs === {
11 * "foo": "bar"
12 * }
13 *
14 * @param {obj} object data to display
15 * @returns {null}
16 */
17function styledJSON (obj) {
18 let json = JSON.stringify(obj, null, 2)
19 if (cli.color.enabled) {
20 let cardinal = require('cardinal')
21 let theme = require('cardinal/themes/jq')
22 cli.log(cardinal.highlight(json, {json: true, theme: theme}))
23 } else {
24 cli.log(json)
25 }
26}
27
28/**
29 * styledHeader logs in a consistent header style
30 *
31 * @example
32 * styledHeader('MyApp') # Outputs === MyApp
33 *
34 * @param {header} header text
35 * @returns {null}
36 */
37function styledHeader (header) {
38 cli.log(cli.color.dim('=== ') + cli.color.bold(header))
39}
40
41/**
42 * styledObject logs an object in a consistent columnar style
43 *
44 * @example
45 * styledObject({name: "myapp", collaborators: ["user1@example.com", "user2@example.com"]})
46 * Collaborators: user1@example.com
47 * user2@example.com
48 * Name: myapp
49 *
50 * @param {obj} object data to print
51 * @param {keys} optional array of keys to sort/filter output
52 * @returns {null}
53 */
54function styledObject (obj, keys) {
55 let keyLengths = Object.keys(obj).map(key => key.toString().length)
56 let maxKeyLength = Math.max.apply(Math, keyLengths) + 2
57 function pp (obj) {
58 if (typeof obj === 'string' || typeof obj === 'number') {
59 return obj
60 } else if (typeof obj === 'object') {
61 return Object.keys(obj).map(k => k + ': ' + util.inspect(obj[k])).join(', ')
62 } else {
63 return util.inspect(obj)
64 }
65 }
66 function logKeyValue (key, value) {
67 cli.log(`${key}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value))
68 }
69 for (var key of (keys || Object.keys(obj).sort())) {
70 let value = obj[key]
71 if (Array.isArray(value)) {
72 if (value.length > 0) {
73 logKeyValue(key, value[0])
74 for (var e of value.slice(1)) {
75 cli.log(' '.repeat(maxKeyLength) + pp(e))
76 }
77 }
78 } else if (value !== null && value !== undefined) {
79 logKeyValue(key, value)
80 }
81 }
82}
83
84/**
85 * styledNameValues logs an array of {name: '', values: ['']} objects in a consistent style
86 *
87 * @example
88 * styledNameValues([{name: "Collaborators", values: ["user1@example.com", "user2@example.com"]},
89 * {name: "Name", values: ["myapp"]}])
90 * Collaborators: user1@example.com
91 * user2@example.com
92 * Name: myapp
93 *
94 * @param {nameValues} nameValues
95 * @returns {null}
96 */
97function styledNameValues (nameValues) {
98 let keys = nameValues.map(nameValue => nameValue.name)
99 let keyLengths = keys.map(name => name.toString().length)
100 let maxKeyLength = Math.max.apply(Math, keyLengths) + 2
101 function pp (obj) {
102 if (typeof obj === 'string' || typeof obj === 'number') {
103 return obj
104 } else if (typeof obj === 'object') {
105 return Object.keys(obj).map(k => k + ': ' + cli.inspect(obj[k])).join(', ')
106 } else {
107 return cli.inspect(obj)
108 }
109 }
110 function logKeyValue (key, value) {
111 cli.log(`${key}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value))
112 }
113 for (var nameValue of nameValues) {
114 let value = nameValue.values
115 if (Array.isArray(value)) {
116 if (value.length > 0) {
117 logKeyValue(nameValue.name, value[0])
118 for (var e of value.slice(1)) {
119 cli.log(' '.repeat(maxKeyLength) + pp(e))
120 }
121 }
122 } else if (value !== null && value !== undefined) {
123 logKeyValue(nameValue.name, value)
124 }
125 }
126}
127
128module.exports.styledJSON = styledJSON
129module.exports.styledHeader = styledHeader
130module.exports.styledObject = styledObject
131module.exports.styledNameValues = styledNameValues