1
1
/* eslint-disable indent */
2
- const _ = require ( 'lodash' ) ;
3
- const columnify = require ( 'columnify' ) ;
4
- const yaml = require ( 'js-yaml' ) ;
5
- const draftlog = require ( 'draftlog' ) ;
2
+ const _ = require ( 'lodash' ) ;
3
+ const columnify = require ( 'columnify' ) ;
4
+ const chalk = require ( 'chalk' ) ;
5
+ const yaml = require ( 'js-yaml' ) ;
6
+ const draftlog = require ( 'draftlog' ) ;
7
+ const { Formatter } = require ( '../../../output' ) ;
8
+ const { FormatterRegistry } = require ( '../../../output/formatters' ) ;
9
+
10
+ const NOOP_FORMATTER = new Formatter ( ) ;
11
+
12
+ const prettyOpts = {
13
+ headingTransform : chalk . bold ,
14
+ columnSplitter : ' ' ,
15
+ } ;
6
16
7
17
draftlog . into ( console ) ;
8
18
@@ -16,7 +26,7 @@ const print = (output) => {
16
26
} ;
17
27
18
28
//i tried that this function will be dynamic (with the keys). it is also possible to add an array with all the fields if you think it better
19
- const _printArrayTable = ( data ) => {
29
+ const _printArrayTable = ( data , pretty = false ) => {
20
30
if ( data . length === 0 ) {
21
31
console . log ( 'no available resources' ) ;
22
32
} else {
@@ -30,49 +40,59 @@ const _printArrayTable = (data) => {
30
40
} ) ;
31
41
res . push ( obj ) ;
32
42
} ) ;
33
- const columns = columnify ( res ) ;
43
+ const columns = columnify ( res , pretty ? prettyOpts : { } ) ;
34
44
print ( columns ) ;
35
45
}
36
46
} ;
37
47
38
- const _printSingleTable = ( info ) => {
48
+ const _printSingleTable = ( info , pretty = false ) => {
39
49
const keys = Object . keys ( info ) ;
40
50
const res = [ ] ;
41
51
const obj = [ ] ;
42
52
_ . forEach ( keys , ( key ) => {
43
53
obj [ key . toUpperCase ( ) ] = info [ key ] ;
44
54
} ) ;
45
55
res . push ( obj ) ;
46
- const columns = columnify ( res ) ;
56
+ const columns = columnify ( res , pretty ? prettyOpts : { } ) ;
47
57
print ( columns ) ;
48
58
} ;
49
59
50
60
51
- const specifyOutputForSingle = ( type , enitity ) => {
61
+ // todo: extract output layer from commands
62
+ const specifyOutputForSingle = ( type , entity , pretty = false ) => {
63
+ let formatter = NOOP_FORMATTER ;
64
+ if ( pretty && entity ) {
65
+ formatter = FormatterRegistry . get ( entity . constructor . name ) ;
66
+ }
52
67
switch ( type ) {
53
68
case 'json' :
54
- print ( enitity . toJson ( ) ) ;
69
+ print ( entity . toJson ( ) ) ;
55
70
break ;
56
71
case 'yaml' :
57
- print ( enitity . toYaml ( ) ) ;
72
+ print ( entity . toYaml ( ) ) ;
58
73
break ;
59
74
case 'name' :
60
- print ( enitity . toName ( ) ) ;
75
+ print ( entity . toName ( ) ) ;
61
76
break ;
62
77
case 'wide' :
63
- _printSingleTable ( enitity . toWide ( ) ) ;
78
+ _printSingleTable ( formatter . apply ( entity . toWide ( ) ) , pretty ) ;
64
79
break ;
65
80
default :
66
- _printSingleTable ( enitity . toDefault ( ) ) ;
81
+ _printSingleTable ( formatter . apply ( entity . toDefault ( ) ) , pretty ) ;
67
82
}
68
83
} ;
69
84
70
85
71
- const specifyOutputForArray = ( type , enitities ) => {
86
+ // todo: extract output layer from commands
87
+ const specifyOutputForArray = ( type , entities , pretty = false ) => {
88
+ let formatter = NOOP_FORMATTER ;
89
+ if ( pretty && entities . length ) {
90
+ formatter = FormatterRegistry . get ( entities [ 0 ] . constructor . name ) ;
91
+ }
72
92
switch ( type ) {
73
93
case 'json' :
74
94
const jsonArray = [ ] ;
75
- _ . forEach ( enitities , ( entity ) => {
95
+ _ . forEach ( entities , ( entity ) => {
76
96
jsonArray . push ( entity . info ) ;
77
97
} ) ;
78
98
@@ -87,7 +107,7 @@ const specifyOutputForArray = (type, enitities) => {
87
107
let yamlArray = {
88
108
items : [ ] ,
89
109
} ;
90
- _ . forEach ( enitities , ( entity ) => {
110
+ _ . forEach ( entities , ( entity ) => {
91
111
yamlArray . items . push ( entity . info ) ;
92
112
} ) ;
93
113
@@ -98,28 +118,28 @@ const specifyOutputForArray = (type, enitities) => {
98
118
}
99
119
break ;
100
120
case 'name' :
101
- _ . forEach ( enitities , ( entity ) => {
121
+ _ . forEach ( entities , ( entity ) => {
102
122
console . log ( entity . toName ( ) ) ;
103
123
} ) ;
104
124
break ;
105
125
case 'id' :
106
- _ . forEach ( enitities , ( entity ) => {
126
+ _ . forEach ( entities , ( entity ) => {
107
127
console . log ( entity . toId ( ) ) ;
108
128
} ) ;
109
129
break ;
110
130
case 'wide' :
111
131
const wideArray = [ ] ;
112
- _ . forEach ( enitities , ( entity ) => {
113
- wideArray . push ( entity . toWide ( ) ) ;
132
+ _ . forEach ( entities , ( entity ) => {
133
+ wideArray . push ( formatter . apply ( entity . toWide ( ) ) ) ;
114
134
} ) ;
115
- _printArrayTable ( wideArray ) ;
135
+ _printArrayTable ( wideArray , pretty ) ;
116
136
break ;
117
137
default :
118
138
const defaultArray = [ ] ;
119
- _ . forEach ( enitities , ( entity ) => {
120
- defaultArray . push ( entity . toDefault ( ) ) ;
139
+ _ . forEach ( entities , ( entity ) => {
140
+ defaultArray . push ( formatter . apply ( entity . toDefault ( ) ) ) ;
121
141
} ) ;
122
- _printArrayTable ( defaultArray ) ;
142
+ _printArrayTable ( defaultArray , pretty ) ;
123
143
}
124
144
} ;
125
145
0 commit comments