11/* 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+ } ;
616
717draftlog . into ( console ) ;
818
@@ -16,7 +26,7 @@ const print = (output) => {
1626} ;
1727
1828//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 ) => {
2030 if ( data . length === 0 ) {
2131 console . log ( 'no available resources' ) ;
2232 } else {
@@ -30,49 +40,59 @@ const _printArrayTable = (data) => {
3040 } ) ;
3141 res . push ( obj ) ;
3242 } ) ;
33- const columns = columnify ( res ) ;
43+ const columns = columnify ( res , pretty ? prettyOpts : { } ) ;
3444 print ( columns ) ;
3545 }
3646} ;
3747
38- const _printSingleTable = ( info ) => {
48+ const _printSingleTable = ( info , pretty = false ) => {
3949 const keys = Object . keys ( info ) ;
4050 const res = [ ] ;
4151 const obj = [ ] ;
4252 _ . forEach ( keys , ( key ) => {
4353 obj [ key . toUpperCase ( ) ] = info [ key ] ;
4454 } ) ;
4555 res . push ( obj ) ;
46- const columns = columnify ( res ) ;
56+ const columns = columnify ( res , pretty ? prettyOpts : { } ) ;
4757 print ( columns ) ;
4858} ;
4959
5060
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+ }
5267 switch ( type ) {
5368 case 'json' :
54- print ( enitity . toJson ( ) ) ;
69+ print ( entity . toJson ( ) ) ;
5570 break ;
5671 case 'yaml' :
57- print ( enitity . toYaml ( ) ) ;
72+ print ( entity . toYaml ( ) ) ;
5873 break ;
5974 case 'name' :
60- print ( enitity . toName ( ) ) ;
75+ print ( entity . toName ( ) ) ;
6176 break ;
6277 case 'wide' :
63- _printSingleTable ( enitity . toWide ( ) ) ;
78+ _printSingleTable ( formatter . apply ( entity . toWide ( ) ) , pretty ) ;
6479 break ;
6580 default :
66- _printSingleTable ( enitity . toDefault ( ) ) ;
81+ _printSingleTable ( formatter . apply ( entity . toDefault ( ) ) , pretty ) ;
6782 }
6883} ;
6984
7085
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+ }
7292 switch ( type ) {
7393 case 'json' :
7494 const jsonArray = [ ] ;
75- _ . forEach ( enitities , ( entity ) => {
95+ _ . forEach ( entities , ( entity ) => {
7696 jsonArray . push ( entity . info ) ;
7797 } ) ;
7898
@@ -87,7 +107,7 @@ const specifyOutputForArray = (type, enitities) => {
87107 let yamlArray = {
88108 items : [ ] ,
89109 } ;
90- _ . forEach ( enitities , ( entity ) => {
110+ _ . forEach ( entities , ( entity ) => {
91111 yamlArray . items . push ( entity . info ) ;
92112 } ) ;
93113
@@ -98,28 +118,28 @@ const specifyOutputForArray = (type, enitities) => {
98118 }
99119 break ;
100120 case 'name' :
101- _ . forEach ( enitities , ( entity ) => {
121+ _ . forEach ( entities , ( entity ) => {
102122 console . log ( entity . toName ( ) ) ;
103123 } ) ;
104124 break ;
105125 case 'id' :
106- _ . forEach ( enitities , ( entity ) => {
126+ _ . forEach ( entities , ( entity ) => {
107127 console . log ( entity . toId ( ) ) ;
108128 } ) ;
109129 break ;
110130 case 'wide' :
111131 const wideArray = [ ] ;
112- _ . forEach ( enitities , ( entity ) => {
113- wideArray . push ( entity . toWide ( ) ) ;
132+ _ . forEach ( entities , ( entity ) => {
133+ wideArray . push ( formatter . apply ( entity . toWide ( ) ) ) ;
114134 } ) ;
115- _printArrayTable ( wideArray ) ;
135+ _printArrayTable ( wideArray , pretty ) ;
116136 break ;
117137 default :
118138 const defaultArray = [ ] ;
119- _ . forEach ( enitities , ( entity ) => {
120- defaultArray . push ( entity . toDefault ( ) ) ;
139+ _ . forEach ( entities , ( entity ) => {
140+ defaultArray . push ( formatter . apply ( entity . toDefault ( ) ) ) ;
121141 } ) ;
122- _printArrayTable ( defaultArray ) ;
142+ _printArrayTable ( defaultArray , pretty ) ;
123143 }
124144} ;
125145
0 commit comments