( host: Host, startDir: string, logger?: Logger, )
| 90 | * @returns A promise that resolves to the name of the discovered package manager, or null if none is found. |
| 91 | */ |
| 92 | export async function discover( |
| 93 | host: Host, |
| 94 | startDir: string, |
| 95 | logger?: Logger, |
| 96 | ): Promise<PackageManagerName | null> { |
| 97 | logger?.debug(`Starting package manager discovery in '${startDir}'...`); |
| 98 | let currentDir = startDir; |
| 99 | |
| 100 | while (true) { |
| 101 | const found = await findLockfiles(host, currentDir, logger); |
| 102 | |
| 103 | if (found.size > 0) { |
| 104 | logger?.debug(`Found lockfile(s): [${[...found].join(', ')}]. Applying precedence...`); |
| 105 | for (const packageManager of PACKAGE_MANAGER_PRECEDENCE) { |
| 106 | if (found.has(packageManager)) { |
| 107 | logger?.debug(`Selected '${packageManager}' based on precedence.`); |
| 108 | |
| 109 | return packageManager; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Stop searching if we reach the git repository root. |
| 115 | if (await isDirectory(host, join(currentDir, '.git'))) { |
| 116 | logger?.debug(`Reached repository root at '${currentDir}'. Stopping search.`); |
| 117 | |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | const parentDir = dirname(currentDir); |
| 122 | if (parentDir === currentDir) { |
| 123 | // We have reached the filesystem root. |
| 124 | logger?.debug('Reached filesystem root. No lockfile found.'); |
| 125 | |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | currentDir = parentDir; |
| 130 | } |
| 131 | } |
no test coverage detected