@@ -6,7 +6,7 @@ import resolve from 'resolve';
6
6
7
7
import { KNOWN_IMPORTS , type KnownCodec } from './knownImports' ;
8
8
import { parseSource , type SourceFile } from './sourceFile' ;
9
- import { errorLeft , logError , logInfo } from './error' ;
9
+ import { errorLeft , logInfo } from './error' ;
10
10
11
11
const readFile = promisify ( fs . readFile ) ;
12
12
@@ -63,7 +63,19 @@ export class Project {
63
63
64
64
if ( ! visitedPackages . has ( sym . from ) ) {
65
65
// This is a step that checks if this import has custom codecs, and loads them into known imports
66
- await this . populateCustomCodecs ( baseDir , sym . from ) ;
66
+ const codecs = await this . getCustomCodecs ( baseDir , sym . from ) ;
67
+ if ( E . isLeft ( codecs ) ) {
68
+ return codecs ;
69
+ }
70
+
71
+ if ( Object . keys ( codecs . right ) . length > 0 ) {
72
+ this . knownImports [ sym . from ] = {
73
+ ...codecs . right ,
74
+ ...this . knownImports [ sym . from ] ,
75
+ } ;
76
+
77
+ logInfo ( `Loaded custom codecs for ${ sym . from } ` ) ;
78
+ }
67
79
}
68
80
69
81
visitedPackages . add ( sym . from ) ;
@@ -158,39 +170,44 @@ export class Project {
158
170
return this . types ;
159
171
}
160
172
161
- private async populateCustomCodecs ( basedir : string , packageName : string ) {
173
+ private async getCustomCodecs (
174
+ basedir : string ,
175
+ packageName : string ,
176
+ ) : Promise < E . Either < string , Record < string , KnownCodec > > > {
177
+ let packageJsonPath = '' ;
178
+
162
179
try {
163
- const packageJson = resolve . sync ( `${ packageName } /package.json` , {
180
+ packageJsonPath = resolve . sync ( `${ packageName } /package.json` , {
164
181
basedir,
165
182
extensions : [ '.json' ] ,
166
183
} ) ;
167
- const packageInfo = JSON . parse ( fs . readFileSync ( packageJson , 'utf8' ) ) ;
168
-
169
- if ( packageInfo [ 'customCodecFile' ] ) {
170
- // The package defines their own custom codecs
171
- const customCodecPath = resolve . sync (
172
- `${ packageName } /${ packageInfo [ 'customCodecFile' ] } ` ,
173
- {
174
- basedir,
175
- extensions : [ '.ts' , '.js' ] ,
176
- } ,
177
- ) ;
178
- const module = await import ( customCodecPath ) ;
179
- if ( module . default === undefined ) {
180
- logError ( `Could not find default export in ${ customCodecPath } ` ) ;
181
- return ;
182
- }
183
-
184
- const customCodecs = module . default ( E ) ;
185
- this . knownImports [ packageName ] = {
186
- ...customCodecs ,
187
- ...this . knownImports [ packageName ] ,
188
- } ;
184
+ } catch ( e ) {
185
+ // This should not lead to the failure of the entire project, so return an empty record
186
+ return E . right ( { } ) ;
187
+ }
189
188
190
- logInfo ( `Loaded custom codecs for ${ packageName } ` ) ;
189
+ const packageInfo = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
190
+
191
+ if ( packageInfo [ 'customCodecFile' ] ) {
192
+ // The package defines their own custom codecs
193
+ const customCodecPath = resolve . sync (
194
+ `${ packageName } /${ packageInfo [ 'customCodecFile' ] } ` ,
195
+ {
196
+ basedir,
197
+ extensions : [ '.ts' , '.js' ] ,
198
+ } ,
199
+ ) ;
200
+
201
+ const module = await import ( customCodecPath ) ;
202
+ if ( module . default === undefined ) {
203
+ // Package does not have a default export so we can't use it. Format of the custom codec file is incorrect
204
+ return errorLeft ( `Could not find default export in ${ customCodecPath } ` ) ;
191
205
}
192
- } catch ( e ) {
193
- return ;
206
+
207
+ const customCodecs = module . default ( E ) ;
208
+ return E . right ( customCodecs ) ;
194
209
}
210
+
211
+ return E . right ( { } ) ;
195
212
}
196
213
}
0 commit comments