@@ -20,9 +20,50 @@ export function generateModels(cwd: string, outputDir?: string, settings: { errM
20
20
} ) ;
21
21
}
22
22
23
- export function generateStatementsAndTypes ( cwd : string ) : Promise < void > {
23
+ export const generateModelsWithOptions = ( cwd : string , options : Record < string , any > ) : Promise < void > => new Promise ( ( resolve , reject ) => {
24
+ spawn ( getCLIPath ( ) , [ 'codegen' , 'models' , ...( Object . entries ( options ) . flat ( ) ) ] , { cwd, stripColors : true } ) . run ( ( err : Error ) => {
25
+ if ( ! err ) {
26
+ resolve ( ) ;
27
+ } else {
28
+ reject ( err ) ;
29
+ }
30
+ } ) ;
31
+ } ) ;
32
+
33
+ export function generateStatementsAndTypes ( cwd : string , errorMessage ?: string ) : Promise < void > {
34
+ return new Promise ( ( resolve , reject ) => {
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 > {
24
52
return new Promise ( ( resolve , reject ) => {
25
- spawn ( getCLIPath ( ) , [ 'codegen' ] , { cwd, stripColors : true } )
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 } )
26
67
. run ( ( err : Error ) => {
27
68
if ( ! err ) {
28
69
resolve ( ) ;
@@ -36,7 +77,10 @@ export function generateStatementsAndTypes(cwd: string) : Promise<void> {
36
77
// CLI workflow to add codegen to Amplify project
37
78
export function addCodegen ( cwd : string , settings : any = { } ) : Promise < void > {
38
79
return new Promise ( ( resolve , reject ) => {
39
- 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 } ) ;
40
84
if ( settings . isAPINotAdded ) {
41
85
chain . wait ( "There are no GraphQL APIs available." ) ;
42
86
chain . wait ( "Add by running $amplify api add" ) ;
@@ -156,22 +200,26 @@ export function generateModelIntrospection(cwd: string, settings: { outputDir?:
156
200
}
157
201
158
202
// CLI workflow to add codegen to non-Amplify JS project
159
- export function addCodegenNonAmplifyJS ( cwd : string ) : Promise < void > {
203
+ export function addCodegenNonAmplifyJS ( cwd : string , params : Array < string > , initialFailureMessage ?: string ) : Promise < void > {
160
204
return new Promise ( ( resolve , reject ) => {
161
- const cmdOptions = [ 'codegen' , 'add' , '--apiId' , 'mockapiid' ] ;
162
- const chain = spawn ( getCLIPath ( ) , cmdOptions , { cwd, stripColors : true } ) ;
163
- chain
164
- . wait ( "Choose the type of app that you're building" )
165
- . sendCarriageReturn ( )
166
- . wait ( 'What javascript framework are you using' )
167
- . sendCarriageReturn ( )
168
- . wait ( 'Choose the code generation language target' ) . sendCarriageReturn ( )
169
- . wait ( 'Enter the file name pattern of graphql queries, mutations and subscriptions' )
170
- . sendCarriageReturn ( )
171
- . wait ( 'Do you want to generate/update all possible GraphQL operations' )
172
- . sendLine ( 'y' )
173
- . wait ( 'Enter maximum statement depth [increase from default if your schema is deeply' )
174
- . 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
+ }
175
223
176
224
chain . run ( ( err : Error ) => {
177
225
if ( ! err ) {
@@ -182,3 +230,31 @@ export function addCodegenNonAmplifyJS(cwd: string): Promise<void> {
182
230
} ) ;
183
231
} ) ;
184
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