1- import { Command } from " @contentstack/cli-command" ;
2- import { flags } from " @contentstack/cli-utilities" ;
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" ;
1+ import { Command } from ' @contentstack/cli-command'
2+ import { flags } from ' @contentstack/cli-utilities'
3+ import * as path from ' path'
4+ import * as fs from 'fs'
5+ import { generateTS , graphqlTS } from '@contentstack/types-generator'
6+ import { sanitizePath } from '../lib/helper'
7+ import { StackConnectionConfig } from ' ../types'
88
99function createOutputPath ( outputFile : string ) {
1010 const outputPath = path . resolve (
1111 sanitizePath ( process . cwd ( ) ) ,
1212 sanitizePath ( outputFile ) ,
13- ) ;
14- const dirName = path . dirname ( outputPath ) ;
13+ )
14+ const dirName = path . dirname ( outputPath )
1515
16- fs . mkdirSync ( dirName , { recursive : true } ) ;
16+ fs . mkdirSync ( dirName , { recursive : true } )
1717
18- return outputPath ;
18+ return outputPath
1919}
2020
2121export default class TypeScriptCodeGeneratorCommand extends Command {
22- static description = " Generate TypeScript typings from a Stack" ;
22+ static description = ' Generate TypeScript typings from a Stack' ;
2323
2424 static examples = [
2525 '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"' ,
@@ -30,135 +30,135 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
3030 ] ;
3131
3232 static flags : any = {
33- " token-alias" : flags . string ( {
34- char : "a" ,
35- description : " delivery token alias" ,
33+ ' token-alias' : flags . string ( {
34+ char : 'a' ,
35+ description : ' delivery token alias' ,
3636 hidden : false ,
3737 multiple : false ,
3838 required : true ,
3939 } ) ,
4040
4141 output : flags . string ( {
42- char : "o" ,
43- description : " full path to output" ,
42+ char : 'o' ,
43+ description : ' full path to output' ,
4444 hidden : false ,
4545 multiple : false ,
4646 required : true ,
4747 } ) ,
4848
4949 prefix : flags . string ( {
50- char : "p" ,
50+ char : 'p' ,
5151 description : 'interface prefix, e.g. "I"' ,
5252 hidden : false ,
5353 multiple : false ,
54- default : "" ,
54+ default : '' ,
5555 required : false ,
5656 } ) ,
5757
5858 doc : flags . boolean ( {
59- char : "d" ,
60- description : " include documentation comments" ,
59+ char : 'd' ,
60+ description : ' include documentation comments' ,
6161 default : true ,
6262 allowNo : true ,
6363 } ) ,
6464
6565 branch : flags . string ( {
66- description : " branch" ,
66+ description : ' branch' ,
6767 hidden : false ,
6868 multiple : false ,
6969 } ) ,
7070
71- " include-system-fields" : flags . boolean ( {
72- description : " include system fields in generated types" ,
71+ ' include-system-fields' : flags . boolean ( {
72+ description : ' include system fields in generated types' ,
7373 default : false ,
7474 } ) ,
7575
76- " api-type" : flags . string ( {
77- default : " rest" ,
76+ ' api-type' : flags . string ( {
77+ default : ' rest' ,
7878 multiple : false ,
79- options : [ " rest" , " graphql" ] ,
79+ options : [ ' rest' , ' graphql' ] ,
8080 description :
81- " [Optional] Please enter an API type to generate the type definitions." ,
81+ ' [Optional] Please enter an API type to generate the type definitions.' ,
8282 } ) ,
8383
8484 namespace : flags . string ( {
8585 description :
86- " [Optional]Please enter a namespace for the GraphQL API type to organize the generated types." ,
86+ ' [Optional]Please enter a namespace for the GraphQL API type to organize the generated types.' ,
8787 } ) ,
8888 } ;
8989
9090 async run ( ) {
9191 try {
92- const { flags } = await this . parse ( TypeScriptCodeGeneratorCommand ) ;
92+ const { flags} = await this . parse ( TypeScriptCodeGeneratorCommand )
9393
94- const token = this . getToken ( flags [ " token-alias" ] ) ;
95- const prefix = flags . prefix ;
96- const includeDocumentation = flags . doc ;
97- const filePath = flags . output ;
98- const branch = flags . branch ;
99- const includeSystemFields = flags [ " include-system-fields" ] ;
100- const namespace = flags . namespace ;
94+ const token = this . getToken ( flags [ ' token-alias' ] )
95+ const prefix = flags . prefix
96+ const includeDocumentation = flags . doc
97+ const filePath = flags . output
98+ const branch = flags . branch
99+ const includeSystemFields = flags [ ' include-system-fields' ]
100+ const namespace = flags . namespace
101101
102- const outputPath = createOutputPath ( filePath ) ;
102+ const outputPath = createOutputPath ( filePath )
103103
104- if ( token . type !== " delivery" ) {
104+ if ( token . type !== ' delivery' ) {
105105 this . warn (
106- " Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token." ,
107- ) ;
106+ ' Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.' ,
107+ )
108108 }
109109
110110 if ( ! outputPath || ! outputPath . trim ( ) ) {
111- this . error ( " Please provide an output path." , { exit : 2 } ) ;
111+ this . error ( ' Please provide an output path.' , { exit : 2 } )
112112 }
113113
114114 const config : StackConnectionConfig = {
115115 apiKey : token . apiKey ,
116116 token : token . token ,
117117 region :
118- this . region . name === "NA" ? "us" : this . region . name . toLowerCase ( ) ,
119- environment : token . environment || "" ,
118+ this . region . name === 'NA' ? 'us' : this . region . name . toLowerCase ( ) ,
119+ environment : token . environment || '' ,
120120 branch : branch || undefined ,
121121 host : this . cdaHost ,
122- } ;
122+ }
123123
124124 // Generate the GraphQL schema TypeScript definitions
125- if ( flags [ " api-type" ] === " graphql" ) {
125+ if ( flags [ ' api-type' ] === ' graphql' ) {
126126 try {
127- if ( config . region === "us" ) {
128- config . region = "US" ;
127+ if ( config . region === 'us' ) {
128+ config . region = 'US'
129129 }
130- const result = await graphqlTS ( { ...config , namespace : namespace } ) ;
130+ const result = await graphqlTS ( { ...config , namespace : namespace } )
131131
132- fs . writeFileSync ( outputPath , result ) ;
132+ fs . writeFileSync ( outputPath , result )
133133 this . log (
134134 `Successfully added the GraphQL schema type definitions to '${ outputPath } '.` ,
135- ) ;
135+ )
136136 } catch ( error : any ) {
137- this . error ( error . error_message , { exit : 1 } ) ;
137+ this . error ( error . error_message , { exit : 1 } )
138138 }
139139 } else {
140140 // Generate the Content Types TypeScript definitions
141141 try {
142142 const result = await generateTS ( {
143143 ...config ,
144- tokenType : " delivery" ,
144+ tokenType : ' delivery' ,
145145 includeDocumentation : includeDocumentation ,
146146 prefix,
147147 systemFields : includeSystemFields ,
148- } ) ;
148+ } )
149149
150- fs . writeFileSync ( outputPath , result ) ;
150+ fs . writeFileSync ( outputPath , result )
151151
152152 // -- TODO : Add count support for the number of Content Types generated
153- this . log ( `Successfully added the Content Types to '${ outputPath } '.` ) ;
153+ this . log ( `Successfully added the Content Types to '${ outputPath } '.` )
154154
155155 // this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
156156 } catch ( error : any ) {
157- this . error ( error . error_message , { exit : 1 } ) ;
157+ this . error ( error . error_message , { exit : 1 } )
158158 }
159159 }
160160 } catch ( error : any ) {
161- this . error ( error as any , { exit : 1 } ) ;
161+ this . error ( error as any , { exit : 1 } )
162162 }
163163 }
164164}
0 commit comments