* Searches a directory for lockfiles and returns a set of package managers that correspond to them. * @param host A `Host` instance for interacting with the file system. * @param directory The directory to search. * @param logger An optional logger instance. * @returns A promise that resolves to
( host: Host, directory: string, logger?: Logger, )
| 29 | * @returns A promise that resolves to a set of package manager names. |
| 30 | */ |
| 31 | async function findLockfiles( |
| 32 | host: Host, |
| 33 | directory: string, |
| 34 | logger?: Logger, |
| 35 | ): Promise<Set<PackageManagerName>> { |
| 36 | logger?.debug(`Searching for lockfiles in '${directory}'...`); |
| 37 | |
| 38 | const foundPackageManagers = new Set<PackageManagerName>(); |
| 39 | const checks: Promise<void>[] = []; |
| 40 | |
| 41 | for (const [name, descriptor] of Object.entries(SUPPORTED_PACKAGE_MANAGERS)) { |
| 42 | const manager = name as PackageManagerName; |
| 43 | for (const lockfile of descriptor.lockfiles) { |
| 44 | checks.push( |
| 45 | (async () => { |
| 46 | try { |
| 47 | const path = join(directory, lockfile); |
| 48 | const stats = await host.stat(path); |
| 49 | if (stats.isFile()) { |
| 50 | logger?.debug(` Found '${lockfile}'.`); |
| 51 | foundPackageManagers.add(manager); |
| 52 | } |
| 53 | } catch { |
| 54 | // File does not exist or cannot be accessed. |
| 55 | } |
| 56 | })(), |
| 57 | ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | await Promise.all(checks); |
| 62 | |
| 63 | return foundPackageManagers; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Checks if a given path is a directory. |