Skip to content

Commit 93e1d9c

Browse files
userquinbenmccannMichaelDeBoey
authored
feat: add getUserAgent support (#20)
Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Michaël De Boey <[email protected]>
1 parent 4c347c8 commit 93e1d9c

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,44 @@ yarn add package-manager-detector
2626

2727
### ESM
2828

29+
To check the file system for which package manager is used:
30+
2931
```js
3032
import { detect } from 'package-manager-detector/detect'
3133
```
3234

33-
or sync version
35+
or sync version:
3436

3537
```js
3638
import { detectSync } from 'package-manager-detector/detect'
3739
```
3840

41+
or to get the currently running package manager:
42+
43+
```js
44+
import { getUserAgent } from 'package-manager-detector/detect'
45+
```
46+
3947
### CommonJS
4048

49+
To check the file system for which package manager is used:
50+
4151
```js
4252
const { detect } = require('package-manager-detector/detect')
4353
```
4454

45-
or sync version
55+
or sync version:
4656

4757
```js
4858
const { detectSync } = require('package-manager-detector/detect')
4959
```
5060

61+
or to get the currently running package manager:
62+
63+
```js
64+
import { getUserAgent } = require('package-manager-detector/detect')
65+
```
66+
5167
## Agents and Commands
5268

5369
This package includes package manager agents and their corresponding commands for:

src/detect.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs'
22
import fsPromises from 'node:fs/promises'
33
import path from 'node:path'
44
import process from 'node:process'
5-
import type { Agent, DetectOptions, DetectResult } from './types'
5+
import type { Agent, AgentName, DetectOptions, DetectResult } from './types'
66
import { AGENTS, LOCKS } from './constants'
77

88
/**
@@ -12,6 +12,7 @@ import { AGENTS, LOCKS } from './constants'
1212
*/
1313
export async function detect(options: DetectOptions = {}): Promise<DetectResult | null> {
1414
const { cwd, onUnknown } = options
15+
1516
for (const directory of lookup(cwd)) {
1617
// Look up for lock files
1718
for (const lock of Object.keys(LOCKS)) {
@@ -40,6 +41,7 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
4041
*/
4142
export function detectSync(options: DetectOptions = {}): DetectResult | null {
4243
const { cwd, onUnknown } = options
44+
4345
for (const directory of lookup(cwd)) {
4446
// Look up for lock files
4547
for (const lock of Object.keys(LOCKS)) {
@@ -61,6 +63,21 @@ export function detectSync(options: DetectOptions = {}): DetectResult | null {
6163
return null
6264
}
6365

66+
/**
67+
* Detects the package manager used in the running process.
68+
*
69+
* This method will check for `process.env.npm_config_user_agent`.
70+
*/
71+
export function getUserAgent(): AgentName | null {
72+
const userAgent = process.env.npm_config_user_agent
73+
if (!userAgent) {
74+
return null
75+
}
76+
77+
const name = userAgent.split('/')[0] as AgentName
78+
return AGENTS.includes(name) ? name : null
79+
}
80+
6481
function * lookup(cwd: string = process.cwd()): Generator<string> {
6582
let directory = path.resolve(cwd)
6683
const { root } = path.parse(directory)

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './constants'
22
export * from './types'
33

4-
export { detect, detectSync } from './detect'
4+
export { detect, detectSync, getUserAgent } from './detect'
55
export { resolveCommand, constructCommand, COMMANDS } from './commands'

test/user-agent.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { getUserAgent } from '../src/detect'
3+
4+
describe('get user agent', () => {
5+
beforeEach(() => {
6+
vi.unstubAllEnvs()
7+
})
8+
;[
9+
['npm', 'npm/10.9.0 node/v20.17.0 linux x64 workspaces/false'],
10+
['yarn', 'yarn/1.22.11 npm/? node/v14.17.6 darwin x64'],
11+
['pnpm', 'pnpm/9.12.1 npm/? node/v20.17.0 linux x64'],
12+
['bun', 'bun/1.1.8 npm/? node/v21.6.0 linux x64'],
13+
].forEach(([agent, detection]) => {
14+
it(`${agent} detected with ${detection}`, () => {
15+
vi.stubEnv(
16+
'npm_config_user_agent',
17+
detection,
18+
)
19+
expect(getUserAgent()).toBe(agent)
20+
})
21+
})
22+
})

0 commit comments

Comments
 (0)