@@ -30,9 +30,40 @@ export const generateModelsWithOptions = (cwd: string, options: Record<string, a
30
30
} ) ;
31
31
} ) ;
32
32
33
- export function generateStatementsAndTypes ( cwd : string ) : Promise < void > {
33
+ export function generateStatementsAndTypes ( cwd : string , errorMessage ?: string ) : Promise < void > {
34
34
return new Promise ( ( resolve , reject ) => {
35
- spawn ( getCLIPath ( ) , [ 'codegen' ] , { cwd, stripColors : true } )
35
+ const chain = spawn ( getCLIPath ( ) , [ 'codegen' ] , { cwd, stripColors : true } )
36
+
37
+ if ( errorMessage ) {
38
+ chain . wait ( errorMessage ) ;
39
+ }
40
+
41
+ return chain . run ( ( err : Error ) => {
42
+ if ( ! err ) {
43
+ resolve ( ) ;
44
+ } else {
45
+ reject ( err ) ;
46
+ }
47
+ } )
48
+ } ) ;
49
+ }
50
+
51
+ export function generateStatements ( cwd : string ) : Promise < void > {
52
+ return new Promise ( ( resolve , reject ) => {
53
+ spawn ( getCLIPath ( ) , [ 'codegen' , 'statements' ] , { cwd, stripColors : true } )
54
+ . run ( ( err : Error ) => {
55
+ if ( ! err ) {
56
+ resolve ( ) ;
57
+ } else {
58
+ reject ( err ) ;
59
+ }
60
+ } )
61
+ } ) ;
62
+ }
63
+
64
+ export function generateTypes ( cwd : string ) : Promise < void > {
65
+ return new Promise ( ( resolve , reject ) => {
66
+ spawn ( getCLIPath ( ) , [ 'codegen' , 'types' ] , { cwd, stripColors : true } )
36
67
. run ( ( err : Error ) => {
37
68
if ( ! err ) {
38
69
resolve ( ) ;
@@ -46,7 +77,10 @@ export function generateStatementsAndTypes(cwd: string) : Promise<void> {
46
77
// CLI workflow to add codegen to Amplify project
47
78
export function addCodegen ( cwd : string , settings : any = { } ) : Promise < void > {
48
79
return new Promise ( ( resolve , reject ) => {
49
- const chain = spawn ( getCLIPath ( ) , [ 'codegen' , 'add' ] , { cwd, stripColors : true } ) ;
80
+ const params = settings . params
81
+ ? [ 'codegen' , 'add' , ...settings . params ]
82
+ : [ 'codegen' , 'add' ] ;
83
+ const chain = spawn ( getCLIPath ( ) , params , { cwd, stripColors : true } ) ;
50
84
if ( settings . isAPINotAdded ) {
51
85
chain . wait ( "There are no GraphQL APIs available." ) ;
52
86
chain . wait ( "Add by running $amplify api add" ) ;
@@ -166,22 +200,26 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?:
166
200
}
167
201
168
202
// CLI workflow to add codegen to non-Amplify JS project
169
- export function addCodegenNonAmplifyJS ( cwd : string ) : Promise < void > {
203
+ export function addCodegenNonAmplifyJS ( cwd : string , params : Array < string > , initialFailureMessage ?: string ) : Promise < void > {
170
204
return new Promise ( ( resolve , reject ) => {
171
- const cmdOptions = [ 'codegen' , 'add' , '--apiId' , 'mockapiid' ] ;
172
- const chain = spawn ( getCLIPath ( ) , cmdOptions , { cwd, stripColors : true } ) ;
173
- chain
174
- . wait ( "Choose the type of app that you're building" )
175
- . sendCarriageReturn ( )
176
- . wait ( 'What javascript framework are you using' )
177
- . sendCarriageReturn ( )
178
- . wait ( 'Choose the code generation language target' ) . sendCarriageReturn ( )
179
- . wait ( 'Enter the file name pattern of graphql queries, mutations and subscriptions' )
180
- . sendCarriageReturn ( )
181
- . wait ( 'Do you want to generate/update all possible GraphQL operations' )
182
- . sendLine ( 'y' )
183
- . wait ( 'Enter maximum statement depth [increase from default if your schema is deeply' )
184
- . sendCarriageReturn ( ) ;
205
+ const chain = spawn ( getCLIPath ( ) , [ 'codegen' , 'add' , ...params ] , { cwd, stripColors : true } ) ;
206
+
207
+ if ( initialFailureMessage ) {
208
+ chain . wait ( initialFailureMessage )
209
+ } else {
210
+ chain
211
+ . wait ( "Choose the type of app that you're building" )
212
+ . sendCarriageReturn ( )
213
+ . wait ( 'What javascript framework are you using' )
214
+ . sendCarriageReturn ( )
215
+ . wait ( 'Choose the code generation language target' ) . sendCarriageReturn ( )
216
+ . wait ( 'Enter the file name pattern of graphql queries, mutations and subscriptions' )
217
+ . sendCarriageReturn ( )
218
+ . wait ( 'Do you want to generate/update all possible GraphQL operations' )
219
+ . sendLine ( 'y' )
220
+ . wait ( 'Enter maximum statement depth [increase from default if your schema is deeply' )
221
+ . sendCarriageReturn ( ) ;
222
+ }
185
223
186
224
chain . run ( ( err : Error ) => {
187
225
if ( ! err ) {
@@ -192,3 +230,31 @@ export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
192
230
} ) ;
193
231
} ) ;
194
232
}
233
+
234
+ export function addCodegenNonAmplifyTS ( cwd : string , params : Array < string > , initialFailureMessage ?: string ) : Promise < void > {
235
+ return new Promise ( ( resolve , reject ) => {
236
+ const chain = spawn ( getCLIPath ( ) , [ 'codegen' , 'add' , ...params ] , { cwd, stripColors : true } ) ;
237
+
238
+ if ( initialFailureMessage ) {
239
+ chain . wait ( initialFailureMessage )
240
+ } else {
241
+ chain
242
+ . wait ( "Choose the type of app that you're building" ) . sendCarriageReturn ( )
243
+ . wait ( 'What javascript framework are you using' ) . sendCarriageReturn ( )
244
+ . wait ( 'Choose the code generation language target' ) . sendKeyDown ( ) . sendCarriageReturn ( )
245
+ . wait ( 'Enter the file name pattern of graphql queries, mutations and subscriptions' ) . sendCarriageReturn ( )
246
+ . wait ( 'Do you want to generate/update all possible GraphQL operations' ) . sendLine ( 'y' )
247
+ . wait ( 'Enter maximum statement depth [increase from default if your schema is deeply' ) . sendCarriageReturn ( )
248
+ . wait ( 'Enter the file name for the generated code' ) . sendCarriageReturn ( )
249
+ . wait ( 'Do you want to generate code for your newly created GraphQL API' ) . sendCarriageReturn ( ) ;
250
+ }
251
+
252
+ chain . run ( ( err : Error ) => {
253
+ if ( ! err ) {
254
+ resolve ( ) ;
255
+ } else {
256
+ reject ( err ) ;
257
+ }
258
+ } ) ;
259
+ } ) ;
260
+ }
0 commit comments