( pathToCheck: string, rootDirectory: string, )
| 34 | * @returns True if the path is within the root directory, false otherwise. |
| 35 | */ |
| 36 | export function isWithinRoot( |
| 37 | pathToCheck: string, |
| 38 | rootDirectory: string, |
| 39 | ): boolean { |
| 40 | const normalizedPathToCheck = path.resolve(pathToCheck); |
| 41 | const normalizedRootDirectory = path.resolve(rootDirectory); |
| 42 | |
| 43 | // Ensure the rootDirectory path ends with a separator for correct startsWith comparison, |
| 44 | // unless it's the root path itself (e.g., '/' or 'C:\'). |
| 45 | const rootWithSeparator = |
| 46 | normalizedRootDirectory === path.sep || |
| 47 | normalizedRootDirectory.endsWith(path.sep) |
| 48 | ? normalizedRootDirectory |
| 49 | : normalizedRootDirectory + path.sep; |
| 50 | |
| 51 | return ( |
| 52 | normalizedPathToCheck === normalizedRootDirectory || |
| 53 | normalizedPathToCheck.startsWith(rootWithSeparator) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Determines if a file is likely binary based on content sampling. |
no outgoing calls
no test coverage detected