@@ -14,20 +14,32 @@ export interface PackageJson {
14
14
}
15
15
16
16
/**
17
- * Find directories containing package.json with @sap/cds dependency
18
- * @param filePaths List of CDS file paths to check
19
- * @param codeqlExePath Path to the CodeQL executable (optional)
20
- * @returns Set of directories containing relevant package.json files
17
+ * Find directories containing package.json with a `@sap/cds` dependency.
18
+ * @param filePaths List of CDS file paths to check.
19
+ * @param codeqlExePath Path to the CodeQL executable (optional).
20
+ * @param sourceRoot The source root directory (optional) - Limits the search to
21
+ * never go above this directory.
22
+ * @returns Set of directories containing relevant package.json files.
21
23
*/
22
- export function findPackageJsonDirs ( filePaths : string [ ] , codeqlExePath ?: string ) : Set < string > {
24
+ export function findPackageJsonDirs (
25
+ filePaths : string [ ] ,
26
+ codeqlExePath ?: string ,
27
+ sourceRoot ?: string ,
28
+ ) : Set < string > {
23
29
const packageJsonDirs = new Set < string > ( ) ;
30
+ const absoluteSourceRoot = sourceRoot ? resolve ( sourceRoot ) : undefined ;
24
31
25
32
filePaths . forEach ( file => {
26
33
let dir = dirname ( resolve ( file ) ) ;
27
- const rootDir = dirname ( dir ) ; // Keep track of the root to avoid infinite loop
28
34
29
- while ( dir !== rootDir && rootDir !== dir ) {
30
- // Check until we reach the root directory
35
+ // Check current directory and parent directories for package.json with a
36
+ // dependency on `@sap/cds`. Never look above the source root directory.
37
+ while ( true ) {
38
+ // Stop if we've reached or gone above the source root directory.
39
+ if ( absoluteSourceRoot && ! dir . startsWith ( absoluteSourceRoot ) ) {
40
+ break ;
41
+ }
42
+
31
43
const packageJsonPath = join ( dir , 'package.json' ) ;
32
44
if ( existsSync ( packageJsonPath ) ) {
33
45
try {
0 commit comments