Skip to content

Commit

Permalink
fix: allow detection of hardhat when using bun (#4224)
Browse files Browse the repository at this point in the history
* fix: allow detection of hardhat when using bun

- added the user agent check now that bun supports it when finding the package manager
- since bun does not have an ls command similar to the others, we check all installed packaged (--all is needed in monorepos) and check it it contains hardhat

* chore: tweaks

---------

Co-authored-by: Tom Meagher <[email protected]>
  • Loading branch information
roderik and tmm authored Sep 16, 2024
1 parent 82404c9 commit b0eb89c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-bees-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/cli": patch
---

Fixed package detection for Bun.
21 changes: 17 additions & 4 deletions packages/cli/src/utils/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ export async function getIsPackageInstalled(parameters: {
const { packageName, cwd = process.cwd() } = parameters
try {
const packageManager = await getPackageManager()
const command =
packageManager === 'yarn' ? ['why', packageName] : ['ls', packageName]
const command = (() => {
switch (packageManager) {
case 'yarn':
return ['why', packageName]
case 'bun':
return ['pm', 'ls', '--all']
default:
return ['ls', packageName]
}
})()

const { stdout } = await execa(packageManager, command, { cwd })
if (stdout !== '') return true
return false

// For Bun, we need to check if the package name is in the output
if (packageManager === 'bun') return stdout.includes(packageName)

return stdout !== ''
} catch (_error) {
return false
}
Expand All @@ -26,6 +38,7 @@ export async function getPackageManager(executable?: boolean | undefined) {
// The yarn@^3 user agent includes npm, so yarn must be checked first.
if (userAgent.includes('yarn')) return 'yarn'
if (userAgent.includes('npm')) return executable ? 'npx' : 'npm'
if (userAgent.includes('bun')) return executable ? 'bunx' : 'bun'
}

const packageManager = await detect()
Expand Down

0 comments on commit b0eb89c

Please sign in to comment.