44 * This is the primary entry point for programmatic usage.
55 * The CLI is a thin wrapper around this function.
66 */
7- import path from 'path' ;
7+ import path from 'node: path' ;
88
99import type { GraphQLSDKConfigTarget } from '../types/config' ;
1010import { getConfigOptions } from '../types/config' ;
@@ -79,12 +79,12 @@ export async function generate(
7979 endpoint : config . endpoint || undefined ,
8080 schemaFile : config . schemaFile || undefined ,
8181 db : config . db ,
82- authorization : options . authorization || config . headers ?. [ ' Authorization' ] ,
82+ authorization : options . authorization || config . headers ?. Authorization ,
8383 headers : config . headers ,
8484 } ) ;
8585
8686 // Run pipeline
87- let pipelineResult ;
87+ let pipelineResult : Awaited < ReturnType < typeof runCodegenPipeline > > ;
8888 try {
8989 console . log ( `Fetching schema from ${ source . describe ( ) } ...` ) ;
9090 pipelineResult = await runCodegenPipeline ( {
@@ -115,6 +115,7 @@ export async function generate(
115115
116116 const allFilesWritten : string [ ] = [ ] ;
117117 const bothEnabled = runReactQuery && runOrm ;
118+ const filesToWrite : Array < { path : string ; content : string } > = [ ] ;
118119
119120 // Generate shared types when both are enabled
120121 if ( bothEnabled ) {
@@ -128,28 +129,11 @@ export async function generate(
128129 } ,
129130 config,
130131 } ) ;
131-
132- if ( ! options . dryRun ) {
133- const writeResult = await writeGeneratedFiles (
134- sharedResult . files ,
135- outputRoot ,
136- [ ] ,
137- ) ;
138- if ( ! writeResult . success ) {
139- return {
140- success : false ,
141- message : `Failed to write shared types: ${ writeResult . errors ?. join ( ', ' ) } ` ,
142- output : outputRoot ,
143- errors : writeResult . errors ,
144- } ;
145- }
146- allFilesWritten . push ( ...( writeResult . filesWritten ?? [ ] ) ) ;
147- }
132+ filesToWrite . push ( ...sharedResult . files ) ;
148133 }
149134
150135 // Generate React Query hooks
151136 if ( runReactQuery ) {
152- const hooksDir = path . join ( outputRoot , 'hooks' ) ;
153137 console . log ( 'Generating React Query hooks...' ) ;
154138 const { files } = generateReactQueryFiles ( {
155139 tables,
@@ -161,27 +145,16 @@ export async function generate(
161145 config,
162146 sharedTypesPath : bothEnabled ? '..' : undefined ,
163147 } ) ;
164-
165- if ( ! options . dryRun ) {
166- const writeResult = await writeGeneratedFiles ( files , hooksDir , [
167- 'queries' ,
168- 'mutations' ,
169- ] ) ;
170- if ( ! writeResult . success ) {
171- return {
172- success : false ,
173- message : `Failed to write React Query hooks: ${ writeResult . errors ?. join ( ', ' ) } ` ,
174- output : outputRoot ,
175- errors : writeResult . errors ,
176- } ;
177- }
178- allFilesWritten . push ( ...( writeResult . filesWritten ?? [ ] ) ) ;
179- }
148+ filesToWrite . push (
149+ ...files . map ( ( file ) => ( {
150+ ...file ,
151+ path : path . posix . join ( 'hooks' , file . path ) ,
152+ } ) ) ,
153+ ) ;
180154 }
181155
182156 // Generate ORM client
183157 if ( runOrm ) {
184- const ormDir = path . join ( outputRoot , 'orm' ) ;
185158 console . log ( 'Generating ORM client...' ) ;
186159 const { files } = generateOrmFiles ( {
187160 tables,
@@ -193,38 +166,36 @@ export async function generate(
193166 config,
194167 sharedTypesPath : bothEnabled ? '..' : undefined ,
195168 } ) ;
196-
197- if ( ! options . dryRun ) {
198- const writeResult = await writeGeneratedFiles ( files , ormDir , [
199- 'models' ,
200- 'query' ,
201- 'mutation' ,
202- ] ) ;
203- if ( ! writeResult . success ) {
204- return {
205- success : false ,
206- message : `Failed to write ORM client: ${ writeResult . errors ?. join ( ', ' ) } ` ,
207- output : outputRoot ,
208- errors : writeResult . errors ,
209- } ;
210- }
211- allFilesWritten . push ( ...( writeResult . filesWritten ?? [ ] ) ) ;
212- }
169+ filesToWrite . push (
170+ ...files . map ( ( file ) => ( {
171+ ...file ,
172+ path : path . posix . join ( 'orm' , file . path ) ,
173+ } ) ) ,
174+ ) ;
213175 }
214176
215177 // Generate barrel file at output root
216178 // This re-exports from the appropriate subdirectories based on which generators are enabled
179+ const barrelContent = generateRootBarrel ( {
180+ hasTypes : bothEnabled ,
181+ hasHooks : runReactQuery ,
182+ hasOrm : runOrm ,
183+ } ) ;
184+ filesToWrite . push ( { path : 'index.ts' , content : barrelContent } ) ;
185+
217186 if ( ! options . dryRun ) {
218- const barrelContent = generateRootBarrel ( {
219- hasTypes : bothEnabled ,
220- hasHooks : runReactQuery ,
221- hasOrm : runOrm ,
187+ const writeResult = await writeGeneratedFiles ( filesToWrite , outputRoot , [ ] , {
188+ pruneStaleFiles : true ,
222189 } ) ;
223- await writeGeneratedFiles (
224- [ { path : 'index.ts' , content : barrelContent } ] ,
225- outputRoot ,
226- [ ] ,
227- ) ;
190+ if ( ! writeResult . success ) {
191+ return {
192+ success : false ,
193+ message : `Failed to write generated files: ${ writeResult . errors ?. join ( ', ' ) } ` ,
194+ output : outputRoot ,
195+ errors : writeResult . errors ,
196+ } ;
197+ }
198+ allFilesWritten . push ( ...( writeResult . filesWritten ?? [ ] ) ) ;
228199 }
229200
230201 const generators = [ runReactQuery && 'React Query' , runOrm && 'ORM' ]
0 commit comments