Skip to content

Commit 128b279

Browse files
feat: export getBunGlobalPrefix from crawlers module (#25)
* feat: add bun global package support - Add getBunGlobalPrefix() function to detect bun's global package path - Update getGlobalNodeModulesPaths() to include pnpm, yarn, and bun paths - Export getBunGlobalPrefix() for external use 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * feat: export getBunGlobalPrefix from crawlers module Export the getBunGlobalPrefix function to allow checking bun's global package prefix. Also adds test coverage for the export. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 58c228e commit 128b279

4 files changed

Lines changed: 74 additions & 3 deletions

File tree

src/commands/scan.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,13 @@ describe('crawlers module', () => {
436436
getNpmGlobalPrefix,
437437
getYarnGlobalPrefix,
438438
getPnpmGlobalPrefix,
439+
getBunGlobalPrefix,
439440
} = await import('../crawlers/index.js')
440441

441442
assert.equal(typeof getNpmGlobalPrefix, 'function')
442443
assert.equal(typeof getYarnGlobalPrefix, 'function')
443444
assert.equal(typeof getPnpmGlobalPrefix, 'function')
445+
assert.equal(typeof getBunGlobalPrefix, 'function')
444446
})
445447
})
446448
})

src/crawlers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export {
44
getNpmGlobalPrefix,
55
getYarnGlobalPrefix,
66
getPnpmGlobalPrefix,
7+
getBunGlobalPrefix,
78
} from './npm-crawler.js'

src/crawlers/npm-crawler.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ function getPnpmGlobalPrefix(): string | null {
104104
}
105105
}
106106

107+
/**
108+
* Get the bun global node_modules path
109+
*/
110+
function getBunGlobalPrefix(): string | null {
111+
try {
112+
const binPath = execSync('bun pm bin -g', {
113+
encoding: 'utf-8',
114+
stdio: ['pipe', 'pipe', 'pipe'],
115+
}).trim()
116+
const bunRoot = path.dirname(binPath)
117+
return path.join(bunRoot, 'install', 'global', 'node_modules')
118+
} catch {
119+
return null
120+
}
121+
}
122+
107123
/**
108124
* NPM ecosystem crawler for discovering packages in node_modules
109125
*/
@@ -150,6 +166,12 @@ export class NpmCrawler {
150166
paths.push(yarnPath)
151167
}
152168

169+
// Try bun global path
170+
const bunPath = getBunGlobalPrefix()
171+
if (bunPath) {
172+
paths.push(bunPath)
173+
}
174+
153175
return paths
154176
}
155177

@@ -498,4 +520,9 @@ export class NpmCrawler {
498520
}
499521

500522
// Re-export global prefix functions for backward compatibility
501-
export { getNpmGlobalPrefix, getYarnGlobalPrefix, getPnpmGlobalPrefix }
523+
export {
524+
getNpmGlobalPrefix,
525+
getYarnGlobalPrefix,
526+
getPnpmGlobalPrefix,
527+
getBunGlobalPrefix,
528+
}

src/utils/global-packages.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ export function getPnpmGlobalPrefix(): string | null {
5151
}
5252
}
5353

54+
/**
55+
* Get the bun global node_modules path
56+
* @returns The path to bun's global node_modules directory, or null if not available
57+
*/
58+
export function getBunGlobalPrefix(): string | null {
59+
try {
60+
const binPath = execSync('bun pm bin -g', {
61+
encoding: 'utf-8',
62+
stdio: ['pipe', 'pipe', 'pipe'],
63+
}).trim()
64+
const bunRoot = path.dirname(binPath)
65+
return path.join(bunRoot, 'install', 'global', 'node_modules')
66+
} catch {
67+
return null
68+
}
69+
}
70+
5471
/**
5572
* Get the global node_modules path, with support for custom override
5673
* @param customPrefix - Optional custom path to use instead of auto-detection
@@ -65,15 +82,39 @@ export function getGlobalPrefix(customPrefix?: string): string {
6582

6683
/**
6784
* Get all global node_modules paths for package lookup
68-
* Currently returns npm global path, but could be extended for yarn global, etc.
85+
* Returns paths from all detected package managers (npm, pnpm, yarn, bun)
6986
* @param customPrefix - Optional custom path to use instead of auto-detection
7087
* @returns Array of global node_modules paths
7188
*/
7289
export function getGlobalNodeModulesPaths(customPrefix?: string): string[] {
7390
if (customPrefix) {
7491
return [customPrefix]
7592
}
76-
return [getNpmGlobalPrefix()]
93+
94+
const paths: string[] = []
95+
96+
try {
97+
paths.push(getNpmGlobalPrefix())
98+
} catch {
99+
// npm not available
100+
}
101+
102+
const pnpmPath = getPnpmGlobalPrefix()
103+
if (pnpmPath) {
104+
paths.push(pnpmPath)
105+
}
106+
107+
const yarnPath = getYarnGlobalPrefix()
108+
if (yarnPath) {
109+
paths.push(yarnPath)
110+
}
111+
112+
const bunPath = getBunGlobalPrefix()
113+
if (bunPath) {
114+
paths.push(bunPath)
115+
}
116+
117+
return paths
77118
}
78119

79120
/**

0 commit comments

Comments
 (0)