11import chalk from 'chalk' ;
2- import { Arguments } from 'yargs' ;
3- import { BaseFlags , BASE_CLI_FLAG_NAMES , getRelevantFlags } from '../flags' ;
2+ import { Arguments , Argv } from 'yargs' ;
3+ import { BaseFlags , getRelevantFlags } from '../flags' ;
44import { fetchListOfTemplates } from '../templating/actions' ;
55import { getOraSpinner , setLogLevelByName } from '../utils/logger' ;
6- import { writeOutput } from '../utils/output' ;
6+ import { writeOutput , writeJSONOutput } from '../utils/output' ;
77import { CliInfo } from './types' ;
88
9- export async function handler ( flags : Arguments < BaseFlags > ) : Promise < void > {
9+ export async function handler (
10+ flags : Arguments < BaseFlags & { outputFormat ?: string } >
11+ ) : Promise < void > {
1012 setLogLevelByName ( flags . logLevel ) ;
1113 const spinner = getOraSpinner ( 'Fetching available templates' ) . start ( ) ;
1214
@@ -21,15 +23,29 @@ export async function handler(flags: Arguments<BaseFlags>): Promise<void> {
2123
2224 spinner . stop ( ) ;
2325
24- templates . forEach ( template => {
25- writeOutput (
26- chalk `‣ ${ template . name } ({cyan ${ template . id } })\n {dim ${ template . description } }`
27- ) ;
28- } ) ;
26+ if ( flags . outputFormat === 'json' ) {
27+ writeJSONOutput ( templates ) ;
28+ } else {
29+ templates . forEach ( ( template ) => {
30+ writeOutput (
31+ chalk `‣ ${ template . name } ({cyan ${ template . id } })\n {dim ${ template . description } }`
32+ ) ;
33+ } ) ;
34+ }
2935}
3036
3137export const cliInfo : CliInfo = {
32- options : { ...getRelevantFlags ( [ ... BASE_CLI_FLAG_NAMES ] ) } ,
38+ options : { ...getRelevantFlags ( [ 'output-format' ] ) } ,
3339} ;
40+
41+ function optionBuilder ( yargs : Argv < any > ) : Argv < Arguments < BaseFlags > > {
42+ yargs = Object . keys ( cliInfo . options ) . reduce ( ( yargs , name ) => {
43+ return yargs . option ( name , cliInfo . options [ name ] ) ;
44+ } , yargs ) ;
45+
46+ return yargs ;
47+ }
48+
3449export const command = [ 'list-templates' ] ;
3550export const describe = 'Lists the available Twilio Function templates' ;
51+ export const builder = optionBuilder ;
0 commit comments