11import { Command } from '@contentstack/cli-command'
22import { flags } from '@contentstack/cli-utilities'
3- import { getGlobalFields , stackConnect , StackConnectionConfig , generateGraphQLTypeDef } from '../lib/stack/client'
4- import { ContentType } from '../lib/stack/schema'
5- import tsgenRunner from '../lib/tsgen/runner'
3+ import * as path from 'path'
4+ import * as fs from 'fs'
5+ import { sanitizePath } from '../lib/helper'
6+ import { generateTS , graphqlTS } from '@contentstack/types-generator'
7+ import { StackConnectionConfig } from '../types'
8+
9+ function createOutputPath ( outputFile : string ) {
10+ const outputPath = path . resolve ( sanitizePath ( process . cwd ( ) ) , sanitizePath ( outputFile ) )
11+ const dirName = path . dirname ( outputPath )
12+
13+ fs . mkdirSync ( dirName , { recursive : true } )
14+
15+ return outputPath
16+ }
617
718export default class TypeScriptCodeGeneratorCommand extends Command {
8- static description = 'generate TypeScript typings from a Stack' ;
19+ static description = 'Generate TypeScript typings from a Stack' ;
920
1021 static examples = [
1122 '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"' ,
@@ -78,11 +89,13 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
7889 const token = this . getToken ( flags [ 'token-alias' ] )
7990 const prefix = flags . prefix
8091 const includeDocumentation = flags . doc
81- const outputPath = flags . output
92+ const filePath = flags . output
8293 const branch = flags . branch
8394 const includeSystemFields = flags [ 'include-system-fields' ]
8495 const namespace = flags . namespace
8596
97+ const outputPath = createOutputPath ( filePath )
98+
8699 if ( token . type !== 'delivery' ) {
87100 this . warn ( 'Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.' )
88101 }
@@ -91,38 +104,61 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
91104 this . error ( 'Please provide an output path.' , { exit : 2 } )
92105 }
93106
107+ let region = ''
108+ switch ( this . region . name ) {
109+ case 'NA' :
110+ region = 'US'
111+ break
112+ case 'AZURE-NA' :
113+ region = 'AZURE_NA'
114+ break
115+ case 'AZURE-EU' :
116+ region = 'AZURE_EU'
117+ break
118+ case 'GCP-NA' :
119+ region = 'GCP_NA'
120+ break
121+ default :
122+ region = this . region . name . toUpperCase ( )
123+ }
124+
94125 const config : StackConnectionConfig = {
95126 apiKey : token . apiKey ,
96127 token : token . token ,
97- region : ( this . region . name === 'NA' ) ? 'us' : this . region . name . toLowerCase ( ) ,
128+ region : region ,
98129 environment : token . environment || '' ,
99- branch : branch || null ,
130+ branch : branch || undefined ,
100131 }
101132
133+ // Generate the GraphQL schema TypeScript definitions
102134 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.' )
135+ try {
136+ const result = await graphqlTS ( { ...config , namespace : namespace } )
137+
138+ fs . writeFileSync ( outputPath , result )
139+ this . log ( `Successfully added the GraphQL schema type definitions to '${ outputPath } '.` )
140+ } catch ( error : any ) {
141+ this . error ( error . error_message , { exit : 1 } )
108142 }
109143 } else {
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.' )
144+ // Generate the Content Types TypeScript definitions
145+ try {
146+ const result = await generateTS ( {
147+ ...config ,
148+ tokenType : 'delivery' ,
149+ includeDocumentation : includeDocumentation ,
150+ prefix,
151+ systemFields : includeSystemFields ,
152+ } )
153+
154+ fs . writeFileSync ( outputPath , result )
155+
156+ // -- TODO : Add count support for the number of Content Types generated
157+ this . log ( `Successfully added the Content Types to '${ outputPath } '.` )
158+
159+ // this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
160+ } catch ( error : any ) {
161+ this . error ( error . error_message , { exit : 1 } )
126162 }
127163 }
128164 } catch ( error : any ) {
0 commit comments