1
- import chalk from 'chalk' ;
2
- import { stripIndent } from 'common-tags' ;
3
- import title from 'title' ;
4
- import logSymbols from 'log-symbols' ;
5
- import columnify from 'columnify' ;
6
- import startCase from 'lodash.startcase' ;
7
-
8
- import { shouldPrettyPrint , supportsEmoji } from './utils' ;
9
- import { ListConfig } from '../commands/list' ;
10
1
import {
11
- ListResult ,
12
- FunctionVersion ,
13
2
AssetVersion ,
14
- VariableResource ,
15
- EnvironmentResource ,
16
- ServiceResource ,
17
3
BuildResource ,
4
+ EnvironmentResource ,
5
+ FunctionVersion ,
18
6
ListOptions ,
7
+ ListResult ,
8
+ ServiceResource ,
9
+ VariableResource ,
19
10
} from '@twilio-labs/serverless-api' ;
11
+ import chalk from 'chalk' ;
12
+ import columnify from 'columnify' ;
13
+ import { stripIndent } from 'common-tags' ;
14
+ import startCase from 'lodash.startcase' ;
15
+ import logSymbols from 'log-symbols' ;
16
+ import title from 'title' ;
17
+ import size from 'window-size' ;
18
+ import { ListConfig } from '../commands/list' ;
19
+ import { shouldPrettyPrint } from './utils' ;
20
20
21
21
type KeyMaps = {
22
22
[ key in ListOptions ] : string [ ] ;
23
23
} ;
24
24
25
+ const LONG_LINE = '─' . repeat ( size . width ) ;
26
+
27
+ function basicIndent ( str : string , repeat = 0 ) {
28
+ return str
29
+ . split ( '\n' )
30
+ . map ( s => ' ' . repeat ( repeat ) + s )
31
+ . join ( '\n' ) ;
32
+ }
33
+
25
34
const baseKeys : KeyMaps = {
26
35
environments : [
27
36
'sid' ,
@@ -103,6 +112,13 @@ function formatDate(dateStr: string) {
103
112
return new Date ( dateStr ) . toString ( ) ;
104
113
}
105
114
115
+ function sortByAccess ( resA : CommonType , resB : CommonType ) {
116
+ if ( resA . visibility === resB . visibility ) {
117
+ resA . path . localeCompare ( resB . path ) ;
118
+ }
119
+ return resA . visibility . localeCompare ( resB . visibility ) ;
120
+ }
121
+
106
122
const headingTransform = ( name : string ) => {
107
123
return chalk . cyan . bold ( startCase ( name ) . replace ( / S i d $ / g, 'SID' ) ) ;
108
124
} ;
@@ -170,26 +186,20 @@ export type FunctionOrAssetContent = {
170
186
function prettyPrintFunctionsOrAssets ( result : FunctionOrAssetContent ) {
171
187
const { entries, environmentSid } = result ;
172
188
const resourceString = entries
189
+ . sort ( sortByAccess )
173
190
. map < string > ( ( entry : CommonType , idx : number ) : string => {
174
191
const symbol = idx + 1 === entries . length ? '└──' : '├──' ;
175
- let emoji = '' ;
176
- if ( supportsEmoji ) {
177
- emoji =
178
- entry . visibility === 'public'
179
- ? '🌐'
180
- : entry . visibility === 'protected'
181
- ? '🔒'
182
- : '🙈' ;
183
- }
192
+ const suffix =
193
+ entry . visibility === 'public'
194
+ ? ' '
195
+ : chalk `{dim [Visibility {reset.bold ${ entry . visibility } }]}` ;
184
196
return stripIndent ( chalk `
185
- │ ${ symbol } ${ emoji } ${ entry . path } {dim [Visibility: ${ entry . visibility } ] }
186
- ` ) ;
197
+ {dim │} {reset ${ entry . path } } ${ suffix }
198
+ ` ) ;
187
199
} )
188
200
. join ( '\n' ) ;
189
201
190
- return stripIndent ( chalk `
191
- │ {bold For Environment:} ${ environmentSid } \n${ resourceString }
192
- ` ) ;
202
+ return resourceString ;
193
203
}
194
204
195
205
type VariablesContent = {
@@ -198,82 +208,94 @@ type VariablesContent = {
198
208
} ;
199
209
200
210
function prettyPrintVariables ( variables : VariablesContent ) {
201
- const { entries, environmentSid } = variables ;
211
+ const { entries } = variables ;
202
212
203
- const variableString = entries
204
- . map ( ( entry , idx ) => {
205
- const symbol = idx + 1 === entries . length ? '└──' : '├──' ;
206
- return stripIndent ( chalk `
207
- │ ${ symbol } {bold ${ entry . key } :} ${ entry . value }
208
- ` ) ;
209
- } )
210
- . join ( '\n' ) ;
213
+ const updatedRows = entries . map ( ( entry : VariableResource ) => {
214
+ return {
215
+ ...entry ,
216
+ key : chalk `{dim │} {cyan ${ entry . key } }:` ,
217
+ } ;
218
+ } ) ;
211
219
212
- return stripIndent ( chalk `
213
- │ {bold For Environment:} ${ environmentSid } \n${ variableString }
214
- ` ) ;
220
+ const renderedValues = columnify ( updatedRows , {
221
+ columns : [ 'key' , 'value' ] ,
222
+ showHeaders : false ,
223
+ } ) ;
224
+
225
+ return renderedValues ;
215
226
}
216
227
217
228
function prettyPrintEnvironment ( environment : EnvironmentResource ) : string {
218
- return stripIndent ( chalk `
219
- │ ${ environment . unique_name } (${ environment . domain_suffix } )
220
- │ ├── {bold SID:} ${ environment . sid }
221
- │ ├── {bold URL:} ${ environment . domain_name }
222
- │ ├── {bold Active Build:} ${ environment . build_sid }
223
- │ └── {bold Last Updated:} ${ formatDate ( environment . date_updated ) }
224
- ` ) ;
229
+ return basicIndent (
230
+ stripIndent ( chalk `
231
+ {bold ${ environment . domain_suffix } } {dim [${ environment . sid } ]}
232
+ {dim │} {cyan URL: } {reset ${ environment . domain_name } }
233
+ {dim │} {cyan Unique Name: } {reset ${ environment . unique_name } }
234
+ {dim │} {cyan Active Build:} {reset ${ environment . build_sid } }
235
+ {dim │} {cyan Last Updated:} {reset ${ formatDate (
236
+ environment . date_updated
237
+ ) } }
238
+ ` )
239
+ ) ;
225
240
}
226
241
227
242
function prettyPrintServices ( service : ServiceResource ) : string {
228
243
return stripIndent ( chalk `
229
- │
230
- │ {cyan.bold ${ service . unique_name } }
231
- │ ├── {bold SID: } ${ service . sid }
232
- │ ├── {bold Created: } ${ formatDate ( service . date_created ) }
233
- │ └── {bold Updated: } ${ formatDate ( service . date_updated ) }
244
+ {bold ${ service . unique_name } }
245
+ {dim │} {cyan SID: } ${ service . sid }
246
+ {dim │} {cyan Created: } {dim ${ formatDate ( service . date_created ) } }
247
+ {dim │} {cyan Updated: } {dim ${ formatDate ( service . date_updated ) } }
234
248
` ) ;
235
249
}
236
250
237
251
function prettyPrintBuilds ( build : BuildResource ) : string {
238
- let status = chalk . yellow ( build . status ) ;
252
+ let status = chalk . reset . yellow ( build . status ) ;
239
253
if ( build . status === 'completed' ) {
240
- status = chalk . green ( `${ logSymbols . success } ${ build . status } ` ) ;
254
+ status = chalk . reset . green ( `${ logSymbols . success } ${ build . status } ` ) ;
241
255
} else if ( build . status === 'failed' ) {
242
- status = chalk . red ( `${ logSymbols . error } ${ build . status } ` ) ;
256
+ status = chalk . reset . red ( `${ logSymbols . error } ${ build . status } ` ) ;
243
257
}
244
- return stripIndent `
245
- │ ${ build . sid } (${ status } )
246
- │ └── ${ chalk `{bold Date:}` } ${ formatDate ( build . date_updated ) }
247
- ` ;
258
+ return basicIndent (
259
+ stripIndent ( chalk `
260
+ {bold ${ build . sid } } {dim [${ status } ]}
261
+ {dim │} {cyan Date:} {dim ${ formatDate ( build . date_updated ) } }
262
+ ` )
263
+ ) ;
248
264
}
249
265
250
266
function prettyPrintSection < T extends ListOptions > (
251
267
sectionTitle : ListOptions ,
252
268
sectionContent : ListResult [ T ]
253
269
) : string {
254
- const sectionHeader = chalk . cyan . bold ( `${ title ( sectionTitle ) } : ` ) ;
270
+ let sectionHeader = chalk . cyan . bold ( `${ title ( sectionTitle ) } :` ) ;
255
271
let content = '' ;
256
272
if ( sectionTitle === 'builds' ) {
257
273
content = ( sectionContent as BuildResource [ ] )
258
274
. map ( prettyPrintBuilds )
259
- . join ( '\n' ) ;
275
+ . join ( `\n\n` ) ;
260
276
} else if ( sectionTitle === 'environments' ) {
261
277
content = ( sectionContent as EnvironmentResource [ ] )
262
278
. map ( prettyPrintEnvironment )
263
- . join ( '\n' ) ;
279
+ . join ( '\n\n ' ) ;
264
280
} else if ( sectionTitle === 'services' ) {
265
281
content = ( sectionContent as ServiceResource [ ] )
266
282
. map ( prettyPrintServices )
267
- . join ( '\n' ) ;
283
+ . join ( '\n\n ' ) ;
268
284
} else if ( sectionTitle === 'variables' ) {
269
- content = prettyPrintVariables ( sectionContent as VariablesContent ) ;
285
+ const data = sectionContent as VariablesContent ;
286
+ sectionHeader = chalk `{cyan.bold ${ title (
287
+ sectionTitle
288
+ ) } } {dim for environment ${ data . environmentSid } }`;
289
+ content = prettyPrintVariables ( data ) ;
270
290
} else if ( sectionTitle === 'functions' || sectionTitle === 'assets' ) {
271
- content = prettyPrintFunctionsOrAssets (
272
- ( sectionContent as unknown ) as FunctionOrAssetContent
273
- ) ;
291
+ const data = sectionContent as FunctionOrAssetContent ;
292
+ sectionHeader = chalk `{cyan.bold ${ title (
293
+ sectionTitle
294
+ ) } } {dim for environment ${ data . environmentSid } }`;
295
+ content = prettyPrintFunctionsOrAssets ( data ) ;
274
296
}
275
297
const output = stripIndent `
276
- ${ sectionHeader } \n${ content }
298
+ ${ sectionHeader } \n\n ${ content } \n\n ${ chalk . dim ( LONG_LINE ) } \n
277
299
` ;
278
300
return output ;
279
301
}
0 commit comments