Skip to content

Commit

Permalink
fix(shadcn-ui): use @antfu/ni to detect package manager (#677)
Browse files Browse the repository at this point in the history
* fix(cli): use @antfu/ni to detect package manager

* chore(cli): cleanup imports

* Create cyan-houses-dress.md

---------

Co-authored-by: shadcn <[email protected]>
  • Loading branch information
Xenfo and shadcn committed Jun 24, 2023
1 parent f834862 commit 0f84973
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.
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.

0 comments on commit 0f84973

Please sign in to comment.