@@ -54,12 +54,23 @@ export class Project {
5454 this . add ( path , sourceFile ) ;
5555
5656 for ( const sym of Object . values ( sourceFile . symbols . imports ) ) {
57- const filePath = p . dirname ( path ) ;
58- const absImportPathE = this . resolve ( filePath , sym . from ) ;
59- if ( E . isLeft ( absImportPathE ) ) {
60- return absImportPathE ;
61- } else if ( ! this . has ( absImportPathE . right ) ) {
62- queue . push ( absImportPathE . right ) ;
57+ if ( ! sym . from . startsWith ( '.' ) ) {
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+ }
6374 }
6475 }
6576 for ( const starExport of sourceFile . symbols . exportStarFiles ) {
@@ -80,66 +91,45 @@ export class Project {
8091 return await readFile ( filename , 'utf8' ) ;
8192 }
8293
83- private resolvePath ( path : string , basedir : string ) : E . Either < string , string > {
94+ resolveEntryPoint ( basedir : string , library : string ) : E . Either < string , string > {
8495 try {
85- const result = resolve . sync ( path , {
96+ const packageJson = resolve . sync ( ` ${ library } /package.json` , {
8697 basedir,
87- extensions : [ '.ts' , '.js' , '.d.ts '] ,
98+ extensions : [ '.json ' ] ,
8899 } ) ;
100+ const packageInfo = JSON . parse ( fs . readFileSync ( packageJson , 'utf8' ) ) ;
89101
90- return E . right ( result ) ;
91- } catch ( e : unknown ) {
92- if ( e instanceof Error && e . message ) {
93- return E . left ( e . message ) ;
102+ let typesEntryPoint = '' ;
103+
104+ if ( packageInfo [ 'types' ] ) {
105+ typesEntryPoint = packageInfo [ 'types' ] ;
94106 }
95107
96- return E . left ( JSON . stringify ( e ) ) ;
97- }
98- }
108+ if ( packageInfo [ 'typings' ] ) {
109+ typesEntryPoint = packageInfo [ 'typings' ] ;
110+ }
99111
100- private findSourceFileFromPackage ( path : string ) : E . Either < string , string > {
101- const mapName = path . replace ( '.js' , '.js.map' ) ;
112+ if ( ! typesEntryPoint ) {
113+ return E . left ( `Could not find types entry point for ${ library } ` ) ;
114+ }
102115
103- if ( fs . existsSync ( mapName ) ) {
104- const mapJson = JSON . parse ( fs . readFileSync ( mapName , 'utf8' ) ) ;
105- const dirName = p . dirname ( path ) ;
106- const source = mapJson . sources [ 0 ] ;
107- const response = resolve . sync ( source , { basedir : dirName } ) ;
108- return E . right ( response ) ;
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 } ` ) ;
109123 }
110-
111- return E . left ( 'Map file not found for ' + path ) ;
112124 }
113125
114126 resolve ( basedir : string , path : string ) : E . Either < string , string > {
115- const BITGO_PREFIX = '@bitgo' ;
116127 try {
117- let resolved = this . resolvePath ( path , basedir ) ;
118- if ( E . isLeft ( resolved ) ) {
119- // Could not resolve the path, try resolving in the types package
120- resolved = this . resolvePath ( '@types/' + path , basedir ) ;
121- }
122-
123- // Types package wasn't found, return an error
124- if ( E . isLeft ( resolved ) ) {
125- return E . left ( 'Could not resolve ' + path + ' from ' + basedir ) ;
126- }
127-
128- const result = resolved . right ;
129-
130- // If we are parsing an internal type package, we want to return the path to the source TS file
131- if ( path . startsWith ( BITGO_PREFIX ) ) {
132- return this . findSourceFileFromPackage ( result ) ;
133- } else {
134- // Else - find the declaration file and return it if it exists
135- const dTsName = result . replace ( '.js' , '.d.ts' ) ;
136-
137- if ( fs . existsSync ( dTsName ) ) {
138- return E . right ( dTsName ) ;
139- }
140-
141- return E . right ( result ) ;
142- }
128+ const result = resolve . sync ( path , {
129+ basedir,
130+ extensions : [ '.ts' , '.js' ] ,
131+ } ) ;
132+ return E . right ( result ) ;
143133 } catch ( e : unknown ) {
144134 if ( e instanceof Error && e . message ) {
145135 return E . left ( e . message ) ;
0 commit comments