Repository: tailwindlabs/tailwindcss
File: packages/@tailwindcss-upgrade/src/utils/packages.ts (TypeScript)

Code before:
    },
    async remove(packages: string[]) {
      let packageManager = await packageManagerForBase.get(base)
      return packageManager.remove(packages)
    },
  }
}

class PackageManager {
  constructor(private base: string) {}

  async exec(command: string) {
    return exec(command, { cwd: this.base })
  }

  async add(
    packages: string[],
    location: 'dependencies' | 'devDependencies',
  ): ReturnType<typeof this.exec> {
    throw new Error('Method not implemented.')
  }

  async remove(packages: string[]): ReturnType<typeof this.exec> {
    throw new Error('Method not implemented.')
  }
}

class BunPackageManager extends PackageManager {
  add(packages: string[], location: 'dependencies' | 'devDependencies') {
    let args = packages.slice()

    if (location === 'devDependencies') {
      args.unshift('--development')
    }

    return this.exec(`bun add ${args.join(' ')}`)
  }

  remove(packages: string[]) {
    return this.exec(`bun remove ${packages.join(' ')}`)
  }
}

class YarnPackageManager extends PackageManager {
  add(packages: string[], location: 'dependencies' | 'devDependencies') {
    let args = packages.slice()

    if (location === 'devDependencies') {
      args.unshift('--dev')
    }


Review comment (refactor):
Could we simplify all this subclassing stuff by just relying on the fact that `{packageManager} add {library} -D` works to save a dev dependency in npm, pnpm, yarn, and bun?

Then it's more like the old implementation which was a lot less code.

Code after:
export function pkg(base: string) {
  return {
    async add(packages: string[], location: 'dependencies' | 'devDependencies' = 'dependencies') {
      let packageManager = await packageManagerForBase.get(base)
      let args = packages.slice()
      if (location === 'devDependencies') {
        args.push(SAVE_DEV[packageManager] || SAVE_DEV.default)
      }
      return exec(`${packageManager} add ${args.join(' ')}`, { cwd: base })
    },
    async remove(packages: string[]) {
      let packageManager = await packageManagerForBase.get(base)
      return exec(`${packageManager} remove ${packages.join(' ')}`, { cwd: base })
    },
  }
}

let didWarnAboutPackageManager = false
let packageManagerForBase = new DefaultMap(async (base) => {
  do {
    // 1. Check package.json for a `packageManager` field
    let packageJsonPath = resolve(base, 'package.json')
    try {
      let packageJsonContent = await fs.readFile(packageJsonPath, 'utf-8')
      let packageJson = JSON.parse(packageJsonContent)
      if (packageJson.packageManager) {
        if (packageJson.packageManager.includes('bun')) {
          return 'bun'
        }
        if (packageJson.packageManager.includes('yarn')) {
          return 'yarn'
        }
        if (packageJson.packageManager.includes('pnpm')) {
          return 'pnpm'
        }
        if (packageJson.packageManager.includes('npm')) {
          return 'npm'
        }
      }
    } catch {}

    // 2. Check for common lockfiles
    try {
      await fs.access(resolve(base, 'bun.lockb'))
      return 'bun'
    } catch {}
    try {
      await fs.access(resolve(base, 'bun.lock'))
      return 'bun'
    } catch {}
    try {