@@ -7,7 +7,7 @@ import prettier from 'prettier';
7
7
// @ts -expect-error TODO: type out if necessary
8
8
import uncrashable from '@float-capital/float-subgraph-uncrashable/src/Index.bs.js' ;
9
9
import DataSourceTemplateCodeGenerator from './codegen/template' ;
10
- import { GENERATED_FILE_NOTE } from './codegen/typescript' ;
10
+ import { GENERATED_FILE_NOTE , ModuleImports } from './codegen/typescript' ;
11
11
import { displayPath } from './command-helpers/fs' ;
12
12
import { Spinner , step , withSpinner } from './command-helpers/spinner' ;
13
13
import debug from './debug' ;
@@ -190,6 +190,7 @@ export default class TypeGenerator {
190
190
}
191
191
192
192
async generateTypesForDataSourceTemplates ( subgraph : immutable . Map < any , any > ) {
193
+ const moduleImports : ModuleImports [ ] = [ ] ;
193
194
return await withSpinner (
194
195
`Generate types for data source templates` ,
195
196
`Failed to generate types for data source templates` ,
@@ -200,22 +201,42 @@ export default class TypeGenerator {
200
201
. get ( 'templates' , immutable . List ( ) )
201
202
. reduce ( ( codeSegments : any , template : any ) => {
202
203
step ( spinner , 'Generate types for data source template' , String ( template . get ( 'name' ) ) ) ;
203
-
204
204
const codeGenerator = new DataSourceTemplateCodeGenerator ( template , this . protocol ) ;
205
205
206
- // Only generate module imports once, because they are identical for
207
- // all types generated for data source templates.
208
- if ( codeSegments . isEmpty ( ) ) {
209
- codeSegments = codeSegments . concat ( codeGenerator . generateModuleImports ( ) ) ;
210
- }
206
+ // we want to get all the imports from the templates
207
+ moduleImports . push ( ...codeGenerator . generateModuleImports ( ) ) ;
211
208
212
209
return codeSegments . concat ( codeGenerator . generateTypes ( ) ) ;
213
210
} , immutable . List ( ) ) ;
214
211
212
+ // we want to dedupe the imports from the templates
213
+ const dedupeModulesImports = moduleImports . reduce (
214
+ ( acc : ModuleImports [ ] , curr : ModuleImports ) => {
215
+ const found = acc . find ( item => item . module === curr . module ) ;
216
+ if ( found ) {
217
+ const foundNames = Array . isArray ( found . nameOrNames )
218
+ ? found . nameOrNames
219
+ : [ found . nameOrNames ] ;
220
+ const currNames = Array . isArray ( curr . nameOrNames )
221
+ ? curr . nameOrNames
222
+ : [ curr . nameOrNames ] ;
223
+ const names = new Set ( [ ...foundNames , ...currNames ] ) ;
224
+ found . nameOrNames = Array . from ( names ) ;
225
+ } else {
226
+ acc . push ( curr ) ;
227
+ }
228
+ return acc ;
229
+ } ,
230
+ [ ] ,
231
+ ) ;
232
+
215
233
if ( ! codeSegments . isEmpty ( ) ) {
216
- const code = prettier . format ( [ GENERATED_FILE_NOTE , ...codeSegments ] . join ( '\n' ) , {
217
- parser : 'typescript' ,
218
- } ) ;
234
+ const code = prettier . format (
235
+ [ GENERATED_FILE_NOTE , ...dedupeModulesImports , ...codeSegments ] . join ( '\n' ) ,
236
+ {
237
+ parser : 'typescript' ,
238
+ } ,
239
+ ) ;
219
240
220
241
const outputFile = path . join ( this . options . outputDir , 'templates.ts' ) ;
221
242
step ( spinner , `Write types for templates to` , displayPath ( outputFile ) ) ;
0 commit comments