@@ -13,10 +13,12 @@ export class Project {
1313 private readonly knownImports : Record < string , Record < string , KnownCodec > > ;
1414
1515 private files : Record < string , SourceFile > ;
16+ private types : Record < string , string > ;
1617
1718 constructor ( files : Record < string , SourceFile > = { } , knownImports = KNOWN_IMPORTS ) {
1819 this . files = files ;
1920 this . knownImports = knownImports ;
21+ this . types = { } ;
2022 }
2123
2224 add ( path : string , sourceFile : SourceFile ) : void {
@@ -42,19 +44,33 @@ export class Project {
4244 const src = await this . readFile ( path ) ;
4345 const sourceFile = await parseSource ( path , src ) ;
4446
47+ if ( sourceFile === undefined ) continue ;
48+
49+ // map types to their file path
50+ for ( const exp of sourceFile . symbols . exports ) {
51+ this . types [ exp . exportedName ] = path ;
52+ }
53+
4554 this . add ( path , sourceFile ) ;
4655
4756 for ( const sym of Object . values ( sourceFile . symbols . imports ) ) {
4857 if ( ! sym . from . startsWith ( '.' ) ) {
49- continue ;
50- }
51-
52- const filePath = p . dirname ( path ) ;
53- const absImportPathE = this . resolve ( filePath , sym . from ) ;
54- if ( E . isLeft ( absImportPathE ) ) {
55- return absImportPathE ;
56- } else if ( ! this . has ( absImportPathE . right ) ) {
57- queue . push ( absImportPathE . right ) ;
58+ // If we are not resolving a relative path, we need to resolve the entry point
59+ const baseDir = p . dirname ( sourceFile . path ) ;
60+ let entryPoint = this . resolveEntryPoint ( baseDir , sym . from ) ;
61+ if ( E . isLeft ( entryPoint ) ) {
62+ continue ;
63+ } else if ( ! this . has ( entryPoint . right ) ) {
64+ queue . push ( entryPoint . right ) ;
65+ }
66+ } else {
67+ const filePath = p . dirname ( path ) ;
68+ const absImportPathE = this . resolve ( filePath , sym . from ) ;
69+ if ( E . isLeft ( absImportPathE ) ) {
70+ return absImportPathE ;
71+ } else if ( ! this . has ( absImportPathE . right ) ) {
72+ queue . push ( absImportPathE . right ) ;
73+ }
5874 }
5975 }
6076 for ( const starExport of sourceFile . symbols . exportStarFiles ) {
@@ -75,24 +91,60 @@ export class Project {
7591 return await readFile ( filename , 'utf8' ) ;
7692 }
7793
94+ resolveEntryPoint ( basedir : string , library : string ) : E . Either < string , string > {
95+ try {
96+ const packageJson = resolve . sync ( `${ library } /package.json` , {
97+ basedir,
98+ extensions : [ '.json' ] ,
99+ } ) ;
100+ const packageInfo = JSON . parse ( fs . readFileSync ( packageJson , 'utf8' ) ) ;
101+
102+ let typesEntryPoint = '' ;
103+
104+ if ( packageInfo [ 'types' ] ) {
105+ typesEntryPoint = packageInfo [ 'types' ] ;
106+ }
107+
108+ if ( packageInfo [ 'typings' ] ) {
109+ typesEntryPoint = packageInfo [ 'typings' ] ;
110+ }
111+
112+ if ( ! typesEntryPoint ) {
113+ return E . left ( `Could not find types entry point for ${ library } ` ) ;
114+ }
115+
116+ const entryPoint = resolve . sync ( `${ library } /${ typesEntryPoint } ` , {
117+ basedir,
118+ extensions : [ '.ts' , '.js' ] ,
119+ } ) ;
120+ return E . right ( entryPoint ) ;
121+ } catch ( err ) {
122+ return E . left ( `Could not resolve entry point for ${ library } : ${ err } ` ) ;
123+ }
124+ }
125+
78126 resolve ( basedir : string , path : string ) : E . Either < string , string > {
79127 try {
80128 const result = resolve . sync ( path , {
81129 basedir,
82130 extensions : [ '.ts' , '.js' ] ,
83131 } ) ;
84132 return E . right ( result ) ;
85- } catch ( e : any ) {
86- if ( typeof e === 'object' && e . hasOwnProperty ( ' message' ) ) {
133+ } catch ( e : unknown ) {
134+ if ( e instanceof Error && e . message ) {
87135 return E . left ( e . message ) ;
88- } else {
89- return E . left ( JSON . stringify ( e ) ) ;
90136 }
137+
138+ return E . left ( JSON . stringify ( e ) ) ;
91139 }
92140 }
93141
94142 resolveKnownImport ( path : string , name : string ) : KnownCodec | undefined {
95143 const baseKey = path . startsWith ( '.' ) ? '.' : path ;
96144 return this . knownImports [ baseKey ] ?. [ name ] ;
97145 }
146+
147+ getTypes ( ) {
148+ return this . types ;
149+ }
98150}
0 commit comments