| 
1 |  | -import { readdir } from '@neodx/fs'  | 
 | 1 | +import { cmdExists } from '@antfu/ni'  | 
 | 2 | +import { ensureFile, scan } from '@neodx/fs'  | 
 | 3 | +import { isTypeOfString, uniq, values } from '@neodx/std'  | 
 | 4 | +import chalk from 'chalk'  | 
 | 5 | +import { execaCommand as $ } from 'execa'  | 
 | 6 | +import { basename, resolve } from 'node:path'  | 
2 | 7 | import type { AbstractPackageManager } from '@/pkg-manager/managers/abstract.pkg-manager'  | 
3 |  | -import { lockFileMatchers } from '@/pkg-manager/pkg-manager.consts'  | 
 | 8 | +import { YarnBerryPackageManager } from '@/pkg-manager/managers/yarn-berry.pkg-manager'  | 
 | 9 | +import {  | 
 | 10 | +  PackageManager,  | 
 | 11 | +  packageManagerMatchers  | 
 | 12 | +} from '@/pkg-manager/pkg-manager.consts'  | 
 | 13 | +import type { PackageJson } from '@/shared/json'  | 
 | 14 | +import { readJson } from '@/shared/json'  | 
 | 15 | +import { addLibraryPrefix } from '@/shared/misc'  | 
4 | 16 | 
 
  | 
5 | 17 | export class PackageManagerFactory {  | 
6 |  | -  // TODO: extend  | 
7 | 18 |   public static async detect(): Promise<AbstractPackageManager> {  | 
8 |  | -    const files = await readdir(process.cwd())  | 
 | 19 | +    let programmaticAgent: PackageManager  | 
9 | 20 | 
 
  | 
10 |  | -    const match = lockFileMatchers.find((matcher) =>  | 
11 |  | -      files.includes(matcher.lockFile)  | 
 | 21 | +    const agents = values(PackageManager)  | 
 | 22 | + | 
 | 23 | +    const lockFilePatterns = await scan(  | 
 | 24 | +      process.cwd(),  | 
 | 25 | +      uniq(packageManagerMatchers.map(({ lockFile }) => lockFile))  | 
12 | 26 |     )  | 
13 | 27 | 
 
  | 
14 |  | -    if (match) {  | 
15 |  | -      return new match.manager()  | 
 | 28 | +    const lockPath = lockFilePatterns.shift() as string  | 
 | 29 | + | 
 | 30 | +    const packageJsonPath = lockPath  | 
 | 31 | +      ? resolve(lockPath, '../package.json')  | 
 | 32 | +      : resolve(process.cwd(), 'package.json')  | 
 | 33 | + | 
 | 34 | +    await ensureFile(lockPath)  | 
 | 35 | +    await ensureFile(packageJsonPath)  | 
 | 36 | + | 
 | 37 | +    const pkg = (await readJson(packageJsonPath)) as PackageJson  | 
 | 38 | + | 
 | 39 | +    if (isTypeOfString(pkg.packageManager)) {  | 
 | 40 | +      const [name, version] = pkg.packageManager.replace(/^\^/, '').split('@')  | 
 | 41 | +      const agent = name as PackageManager  | 
 | 42 | + | 
 | 43 | +      const isYarnBerry =  | 
 | 44 | +        agent === PackageManager.YARN &&  | 
 | 45 | +        version &&  | 
 | 46 | +        Number.parseInt(version, 10) > 1  | 
 | 47 | + | 
 | 48 | +      if (isYarnBerry) {  | 
 | 49 | +        return new YarnBerryPackageManager()  | 
 | 50 | +      }  | 
 | 51 | + | 
 | 52 | +      if (agents.includes(agent)) {  | 
 | 53 | +        programmaticAgent = agent  | 
 | 54 | +      }  | 
 | 55 | +    }  | 
 | 56 | + | 
 | 57 | +    const match = packageManagerMatchers.find(  | 
 | 58 | +      (matcher) =>  | 
 | 59 | +        matcher.lockFile === basename(lockPath) ||  | 
 | 60 | +        programmaticAgent === matcher.name  | 
 | 61 | +    )  | 
 | 62 | + | 
 | 63 | +    if (!match) {  | 
 | 64 | +      throw new Error('Unable to detect a package manager.')  | 
 | 65 | +    }  | 
 | 66 | + | 
 | 67 | +    if (!cmdExists(match.name)) {  | 
 | 68 | +      console.warn(addLibraryPrefix(`${match.name} is not installed.`))  | 
 | 69 | +      console.info(  | 
 | 70 | +        addLibraryPrefix(  | 
 | 71 | +          `Attempting to install ${chalk.cyan(match.name)} globally...`  | 
 | 72 | +        )  | 
 | 73 | +      )  | 
 | 74 | + | 
 | 75 | +      await $(`npm i -g ${match.name}`, {  | 
 | 76 | +        stdio: 'inherit',  | 
 | 77 | +        cwd: process.cwd()  | 
 | 78 | +      })  | 
16 | 79 |     }  | 
17 | 80 | 
 
  | 
18 |  | -    // TODO better errors  | 
19 |  | -    throw new Error('never')  | 
 | 81 | +    return new match.manager()  | 
20 | 82 |   }  | 
21 | 83 | }  | 
0 commit comments