1
1
#!/usr/bin/env node
2
2
3
- import { promises as fs } from 'fs' ;
4
- import * as path from 'path' ;
5
- import { exec } from 'child_process' ;
3
+ import { promises as fs } from 'fs'
4
+ import * as path from 'path'
5
+ import { exec } from 'child_process'
6
6
7
7
// Helper to find the gql-gen binary
8
8
const getGqlGenCommand = ( ) : string => {
9
9
try {
10
10
// Find the graphql-code-generator package that provides gql-gen
11
- const gqlGenPath = require . resolve ( 'graphql-code-generator/dist/cli.js' ) ;
12
- return `node "${ gqlGenPath } "` ;
11
+ const gqlGenPath = require . resolve ( 'graphql-code-generator/dist/cli.js' )
12
+ return `node "${ gqlGenPath } "`
13
13
} catch ( error ) {
14
14
// Fallback to npx with correct package name
15
- console . warn ( 'Could not resolve gql-gen binary, falling back to npx' ) ;
16
- return 'npx graphql-code-generator' ;
15
+ console . warn ( 'Could not resolve gql-gen binary, falling back to npx' )
16
+ return 'npx graphql-code-generator'
17
17
}
18
- } ;
18
+ }
19
19
20
20
// Helper to get argument value from CLI
21
21
const getArgValue = ( argName : string ) : string | undefined = > {
22
- const args = process . argv . slice ( 2 ) ;
23
- const index = args . indexOf ( `--${ argName } ` ) ;
22
+ const args = process . argv . slice ( 2 )
23
+ const index = args . indexOf ( `--${ argName } ` )
24
24
if ( index !== - 1 && args [ index + 1 ] ) {
25
- return args [ index + 1 ] ;
25
+ let value = args [ index + 1 ]
26
+ if ( ( value . startsWith ( "'" ) && value . endsWith ( "'" ) ) || ( value . startsWith ( '"' ) && value . endsWith ( '"' ) ) ) {
27
+ value = value . slice ( 1 , - 1 )
28
+ }
29
+ return value
26
30
}
27
- return undefined ;
28
- } ;
31
+ return undefined
32
+ }
29
33
30
34
// Delete files asynchronously
31
35
const deleteFiles = async ( listFiles : string [ ] ) : Promise < void > => {
32
- await Promise . all ( listFiles . map ( async ( file ) => {
33
- try {
34
- await fs . unlink ( file ) ;
35
- console . log ( `Deleted: ${ file } ` ) ;
36
- } catch ( err ) {
37
- console . error ( `Failed to delete ${ file } :` , err ) ;
38
- }
39
- } ) ) ;
40
- } ;
36
+ await Promise . all (
37
+ listFiles . map ( async ( file ) => {
38
+ try {
39
+ await fs . unlink ( file )
40
+ console . log ( `Deleted: ${ file } ` )
41
+ } catch ( err ) {
42
+ console . error ( `Failed to delete ${ file } :` , err )
43
+ }
44
+ } )
45
+ )
46
+ }
41
47
42
48
// Remove input types region and update namespace/usings
43
49
async function removeInputTypesRegion ( filePath : string ) : Promise < void > {
44
- const fileName = path . parse ( filePath ) . name . replace ( '.generated' , '' ) ;
45
- const folder = path . dirname ( filePath ) . replace ( / \/ / g, '.' ) ;
46
- const namespace = `${ folder } .${ fileName } ` ;
50
+ const fileName = path . parse ( filePath ) . name . replace ( '.generated' , '' )
51
+ const folder = path . dirname ( filePath ) . replace ( / \/ / g, '.' )
52
+ const namespace = `${ folder } .${ fileName } `
47
53
48
- let content = await fs . readFile ( filePath , 'utf8' ) ;
49
- content = content . replace ( / # r e g i o n i n p u t t y p e s [ \s \S ] * ?# e n d r e g i o n [ ^ \n ] * \n ? / g, '' ) ;
50
- content = content . replace ( / u s i n g A g o d a \. C o d e G e n \. G r a p h Q L / g, 'using Agoda.Graphql' ) ;
51
- content = content . replace ( `namespace Generated.${ fileName } ` , `namespace ${ namespace } ` ) ;
52
- content = content . replace ( / s u m a r y / g, " summary" ) ;
54
+ let content = await fs . readFile ( filePath , 'utf8' )
55
+ content = content . replace ( / # r e g i o n i n p u t t y p e s [ \s \S ] * ?# e n d r e g i o n [ ^ \n ] * \n ? / g, '' )
56
+ content = content . replace ( / u s i n g A g o d a \. C o d e G e n \. G r a p h Q L / g, 'using Agoda.Graphql' )
57
+ content = content . replace ( `namespace Generated.${ fileName } ` , `namespace ${ namespace } ` )
58
+ content = content . replace ( / s u m a r y / g, ' summary' )
53
59
54
- await fs . writeFile ( filePath , content , 'utf8' ) ;
60
+ await fs . writeFile ( filePath , content , 'utf8' )
55
61
}
56
62
57
63
// Generate GraphQL code and post-process
58
64
const generateGraphql = async ( schemaUrl : string , filePath : string ) : Promise < void > => {
59
- const folder = path . dirname ( filePath ) ;
60
- const gqlGenCommand = getGqlGenCommand ( ) ;
65
+ const folder = path . dirname ( filePath )
66
+ const gqlGenCommand = getGqlGenCommand ( )
61
67
62
68
await new Promise < void > ( ( resolve , reject ) => {
63
69
exec (
64
- `${ gqlGenCommand } --schema ' ${ schemaUrl } ' --template agoda-graphql-codegen-csharp --out ${ folder } ${ filePath } ` ,
70
+ `${ gqlGenCommand } --schema " ${ schemaUrl } " --template agoda-graphql-codegen-csharp --out " ${ folder } " " ${ filePath } " ` ,
65
71
( error , stdout , stderr ) => {
66
72
if ( error ) {
67
- console . error ( `Error: ${ error . message } ` ) ;
68
- reject ( error ) ;
69
- return ;
73
+ console . error ( `Error: ${ error . message } ` )
74
+ reject ( error )
75
+ return
70
76
}
71
- resolve ( ) ;
77
+ resolve ( )
72
78
}
73
- ) ;
74
- } ) ;
79
+ )
80
+ } )
75
81
76
- const oldPath = path . join ( folder , 'Classes.cs' ) ;
77
- const fileName = `${ path . parse ( filePath ) . name } .generated.cs` ;
78
- const newPath = path . join ( folder , fileName ) ;
82
+ const oldPath = path . join ( folder , 'Classes.cs' )
83
+ const fileName = `${ path . parse ( filePath ) . name } .generated.cs`
84
+ const newPath = path . join ( folder , fileName )
79
85
80
- await fs . rename ( oldPath , newPath ) ;
81
- await removeInputTypesRegion ( newPath ) ;
82
- console . log ( 'generated: ' , newPath ) ;
83
- } ;
86
+ await fs . rename ( oldPath , newPath )
87
+ await removeInputTypesRegion ( newPath )
88
+ console . log ( 'generated: ' , newPath )
89
+ }
84
90
85
91
// Recursively get files with a specific extension
86
- const getGraphqlFiles = async (
87
- dir : string ,
88
- extensionName : string ,
89
- files : string [ ] = [ ]
90
- ) : Promise < string [ ] > => {
91
- const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
92
+ const getGraphqlFiles = async ( dir : string , extensionName : string , files : string [ ] = [ ] ) : Promise < string [ ] > => {
93
+ const entries = await fs . readdir ( dir , { withFileTypes : true } )
92
94
for ( const entry of entries ) {
93
- const fullPath = path . join ( dir , entry . name ) ;
95
+ const fullPath = path . join ( dir , entry . name )
94
96
if ( entry . isDirectory ( ) ) {
95
- await getGraphqlFiles ( fullPath , extensionName , files ) ;
97
+ await getGraphqlFiles ( fullPath , extensionName , files )
96
98
} else if ( entry . isFile ( ) && entry . name . endsWith ( extensionName ) ) {
97
- files . push ( fullPath ) ;
99
+ files . push ( fullPath )
98
100
}
99
101
}
100
- return files ;
101
- } ;
102
+ return files
103
+ }
102
104
103
105
// Main runner
104
106
const run = async ( ) : Promise < void > => {
105
- const rawGraphqlDirectory = getArgValue ( 'graphql-dir' ) ;
106
- const schemaUrl = getArgValue ( 'schema-url' ) ;
107
+ const rawGraphqlDirectory = getArgValue ( 'graphql-dir' )
108
+ const schemaUrl = getArgValue ( 'schema-url' )
107
109
108
110
if ( ! rawGraphqlDirectory || ! schemaUrl ) {
109
- console . error ( 'Usage: script --graphql-dir <dir> --schema-url <url>' ) ;
110
- process . exit ( 1 ) ;
111
+ console . error ( 'Usage: script --graphql-dir <dir> --schema-url <url>' )
112
+ process . exit ( 1 )
111
113
}
112
114
113
- console . log ( 'raw graphql directory: ' , rawGraphqlDirectory ) ;
114
- console . log ( 'graphql schema url: ' , schemaUrl ) ;
115
+ console . log ( 'raw graphql directory: ' , rawGraphqlDirectory )
116
+ console . log ( 'graphql schema url: ' , schemaUrl )
115
117
116
- const oldGeneratedFiles = await getGraphqlFiles ( rawGraphqlDirectory , '.generated.cs' ) ;
117
- console . log ( 'removing generated files...' ) ;
118
- await deleteFiles ( oldGeneratedFiles ) ;
118
+ const oldGeneratedFiles = await getGraphqlFiles ( rawGraphqlDirectory , '.generated.cs' )
119
+ console . log ( 'removing generated files...' )
120
+ await deleteFiles ( oldGeneratedFiles )
119
121
120
- const graphqlFiles = await getGraphqlFiles ( rawGraphqlDirectory , '.graphql' ) ;
122
+ const graphqlFiles = await getGraphqlFiles ( rawGraphqlDirectory , '.graphql' )
121
123
122
124
for ( const f of graphqlFiles ) {
123
- await generateGraphql ( schemaUrl , f ) ;
125
+ await generateGraphql ( schemaUrl , f )
124
126
}
125
- } ;
127
+ }
126
128
127
129
run ( ) . catch ( ( err ) => {
128
- console . error ( 'Fatal error:' , err ) ;
129
- process . exit ( 1 ) ;
130
- } ) ;
130
+ console . error ( 'Fatal error:' , err )
131
+ process . exit ( 1 )
132
+ } )
0 commit comments