@@ -76,16 +76,78 @@ export class RepositoryParser {
7676 }
7777 ) : Promise < void > {
7878 console . log ( 'Single project detected.' ) ;
79+
80+ const projectRoot = this . findPackageJsonRoot ( absolutePath ) ;
81+ console . log ( `Found package.json root at: ${ projectRoot } ` ) ;
82+
7983 this . project = ProjectFactory . createProjectForSingleRepo (
80- this . projectRoot ,
84+ projectRoot ,
8185 this . tsConfigPath ,
8286 this . tsConfigCache
8387 ) ;
84- this . moduleParser = new ModuleParser ( this . project , this . projectRoot ) ;
85- const module = await this . moduleParser . parseModule ( absolutePath , '.' , options ) ;
88+ this . moduleParser = new ModuleParser ( this . project , projectRoot ) ;
89+ const module = await this . moduleParser . parseModule ( projectRoot , '.' , options ) ;
8690 repository . Modules [ module . Name ] = module ;
8791 }
8892
93+ /**
94+ * Find the directory containing package.json by traversing all subdirectories
95+ * @param startPath - The path to start searching from
96+ * @returns The absolute path of the directory containing package.json
97+ */
98+ private findPackageJsonRoot ( startPath : string ) : string {
99+ const result = this . findPackageJsonRecursive ( path . resolve ( startPath ) ) ;
100+
101+ if ( result ) {
102+ console . log ( `Found package.json at: ${ result } ` ) ;
103+ return result ;
104+ }
105+
106+ console . warn ( `No package.json found starting from ${ startPath } , using original path` ) ;
107+ return path . resolve ( startPath ) ;
108+ }
109+
110+ /**
111+ * Recursively search for package.json in directory and all subdirectories
112+ * @param currentPath - Current directory to search
113+ * @returns The absolute path of the directory containing package.json, or null if not found
114+ */
115+ private findPackageJsonRecursive ( currentPath : string ) : string | null {
116+ try {
117+ const packageJsonPath = path . join ( currentPath , 'package.json' ) ;
118+
119+ if ( fs . existsSync ( packageJsonPath ) ) {
120+ return currentPath ;
121+ }
122+
123+ const items = fs . readdirSync ( currentPath , { withFileTypes : true } ) ;
124+
125+ for ( const item of items ) {
126+ if ( ! item . isDirectory ( ) ) {
127+ continue ;
128+ }
129+
130+ const dirName = item . name ;
131+
132+ if ( dirName === 'node_modules' || dirName . startsWith ( '.' ) ) {
133+ continue ;
134+ }
135+
136+ const fullPath = path . join ( currentPath , dirName ) ;
137+ const result = this . findPackageJsonRecursive ( fullPath ) ;
138+
139+ if ( result ) {
140+ return result ;
141+ }
142+ }
143+
144+ return null ;
145+ } catch ( error ) {
146+ console . warn ( `Error searching for package.json in ${ currentPath } :` , error ) ;
147+ return null ;
148+ }
149+ }
150+
89151 private buildGlobalGraph ( repository : Repository ) : void {
90152 GraphBuilder . buildGraph ( repository ) ;
91153 }
0 commit comments