11import { Command } from '@contentstack/cli-command'
22import { FlagInput , flags } from '@contentstack/cli-utilities'
3- import { getGlobalFields , stackConnect , StackConnectionConfig } from '../lib/stack/client'
3+ import { getGlobalFields , stackConnect , StackConnectionConfig , generateGraphQLTypeDef } from '../lib/stack/client'
44import { ContentType } from '../lib/stack/schema'
55import tsgenRunner from '../lib/tsgen/runner'
66
@@ -11,6 +11,8 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
1111 '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"' ,
1212 '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" -p "I"' ,
1313 '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --no-doc' ,
14+ '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql' ,
15+ '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql --namespace "GraphQL" ' ,
1416 ] ;
1517
1618 static flags : FlagInput = {
@@ -56,6 +58,17 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
5658 description : 'include system fields in generated types' ,
5759 default : false ,
5860 } ) ,
61+
62+ 'api-type' : flags . string ( {
63+ default : 'rest' ,
64+ multiple : false ,
65+ options : [ 'rest' , 'graphql' ] ,
66+ description : '[Optional] Please enter an API type to generate the type definitions.' ,
67+ } ) ,
68+
69+ namespace : flags . string ( {
70+ description : '[Optional]Please enter a namespace for the GraphQL API type to organize the generated types.' ,
71+ } ) ,
5972 } ;
6073
6174 async run ( ) {
@@ -68,6 +81,7 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
6881 const outputPath = flags . output
6982 const branch = flags . branch
7083 const includeSystemFields = flags [ 'include-system-fields' ]
84+ const namespace = flags . namespace
7185
7286 if ( token . type !== 'delivery' ) {
7387 this . warn ( 'Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.' )
@@ -85,24 +99,33 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
8599 branch : branch || null ,
86100 }
87101
88- const [ client , globalFields ] = await Promise . all ( [ stackConnect ( this . deliveryAPIClient . Stack , config , this . cdaHost ) , getGlobalFields ( config , this . cdaHost ) ] )
89-
90- let schemas : ContentType [ ] = [ ]
91- if ( client . types ?. length ) {
92- if ( ( globalFields as any ) ?. global_fields ?. length ) {
93- schemas = schemas . concat ( ( globalFields as any ) . global_fields as ContentType )
94- schemas = schemas . map ( schema => ( {
95- ...schema ,
96- schema_type : 'global_field' ,
97- } ) )
102+ if ( flags [ 'api-type' ] === 'graphql' ) {
103+ const result = await generateGraphQLTypeDef ( config , outputPath , namespace )
104+ if ( result ) {
105+ this . log ( `Successfully added the GraphQL schema type definitions to '${ result . outputPath } '.` )
106+ } else {
107+ this . log ( 'No schema found in the stack! Please use a valid stack.' )
98108 }
99- schemas = schemas . concat ( client . types )
100- const result = await tsgenRunner ( outputPath , schemas , prefix , includeDocumentation , includeSystemFields )
101- this . log ( `Wrote ${ result . definitions } Content Types to '${ result . outputPath } '.` )
102109 } else {
103- this . log ( 'No Content Types exist in the Stack.' )
110+ const [ client , globalFields ] = await Promise . all ( [ stackConnect ( this . deliveryAPIClient . Stack , config , this . cdaHost ) , getGlobalFields ( config , this . cdaHost ) ] )
111+
112+ let schemas : ContentType [ ] = [ ]
113+ if ( client . types ?. length ) {
114+ if ( ( globalFields as any ) ?. global_fields ?. length ) {
115+ schemas = schemas . concat ( ( globalFields as any ) . global_fields as ContentType )
116+ schemas = schemas . map ( schema => ( {
117+ ...schema ,
118+ schema_type : 'global_field' ,
119+ } ) )
120+ }
121+ schemas = schemas . concat ( client . types )
122+ const result = await tsgenRunner ( outputPath , schemas , prefix , includeDocumentation , includeSystemFields )
123+ this . log ( `Wrote ${ result . definitions } Content Types to '${ result . outputPath } '.` )
124+ } else {
125+ this . log ( 'No Content Types exist in the Stack.' )
126+ }
104127 }
105- } catch ( error ) {
128+ } catch ( error : any ) {
106129 this . error ( error as any , { exit : 1 } )
107130 }
108131 }
0 commit comments