Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-houses-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-ui": patch
---

fix(cli): use `@antfu/ni` to detect package manager
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"test": "vitest run"
},
"dependencies": {
"@antfu/ni": "^0.21.4",
"chalk": "5.2.0",
"commander": "^10.0.0",
"cosmiconfig": "^8.1.3",
Expand Down
49 changes: 6 additions & 43 deletions packages/cli/src/utils/get-package-manager.ts
Original file line number Diff line number Diff line change
@@ -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")),
])

if (yarnLock) {
return "yarn"
}

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"
}
const packageManager = await detect({ programmatic: true, cwd: targetDir })

if (userAgent.startsWith("pnpm")) {
return "pnpm"
}
if (packageManager === "yarn@berry") return "yarn"
if (packageManager === "pnpm@6") return "pnpm"
if (packageManager === "bun") return "npm"

return "npm"
return packageManager ?? "npm"
}
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.