fix(shadcn-ui): use @antfu/ni to detect package manager (#677)

* fix(cli): use @antfu/ni to detect package manager

* chore(cli): cleanup imports

* Create cyan-houses-dress.md

---------

Co-authored-by: shadcn <m@shadcn.com>
This commit is contained in:
Samuel Corsi-House
2023-06-24 13:01:22 -04:00
committed by GitHub
parent f8348621f4
commit 0f84973b4d
4 changed files with 21 additions and 44 deletions

View File

@@ -1,50 +1,13 @@
import { promises as fs } from "fs"
import path from "path"
async function fileExists(path: string) {
try {
await fs.access(path)
return true
} catch {
return false
}
}
import { detect } from "@antfu/ni"
export async function getPackageManager(
targetDir: string
): Promise<"yarn" | "pnpm" | "npm"> {
const [yarnLock, npmLock, pnpmLock] = await Promise.all([
fileExists(path.resolve(targetDir, "yarn.lock")),
fileExists(path.resolve(targetDir, "package-lock.json")),
fileExists(path.resolve(targetDir, "pnpm-lock.yaml")),
])
const packageManager = await detect({ programmatic: true, cwd: targetDir })
if (yarnLock) {
return "yarn"
}
if (packageManager === "yarn@berry") return "yarn"
if (packageManager === "pnpm@6") return "pnpm"
if (packageManager === "bun") return "npm"
if (pnpmLock) {
return "pnpm"
}
if (npmLock) {
return "npm"
}
// Match based on used package manager
const userAgent = process.env.npm_config_user_agent
if (!userAgent) {
return "npm"
}
if (userAgent.startsWith("yarn")) {
return "yarn"
}
if (userAgent.startsWith("pnpm")) {
return "pnpm"
}
return "npm"
return packageManager ?? "npm"
}