@@ -2,7 +2,7 @@ import fs from 'node:fs'
2
2
import fsPromises from 'node:fs/promises'
3
3
import path from 'node:path'
4
4
import process from 'node:process'
5
- import type { Agent , DetectOptions , DetectResult } from './types'
5
+ import type { Agent , AgentName , DetectOptions , DetectResult } from './types'
6
6
import { AGENTS , LOCKS } from './constants'
7
7
8
8
/**
@@ -12,6 +12,7 @@ import { AGENTS, LOCKS } from './constants'
12
12
*/
13
13
export async function detect ( options : DetectOptions = { } ) : Promise < DetectResult | null > {
14
14
const { cwd, onUnknown } = options
15
+
15
16
for ( const directory of lookup ( cwd ) ) {
16
17
// Look up for lock files
17
18
for ( const lock of Object . keys ( LOCKS ) ) {
@@ -40,6 +41,7 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
40
41
*/
41
42
export function detectSync ( options : DetectOptions = { } ) : DetectResult | null {
42
43
const { cwd, onUnknown } = options
44
+
43
45
for ( const directory of lookup ( cwd ) ) {
44
46
// Look up for lock files
45
47
for ( const lock of Object . keys ( LOCKS ) ) {
@@ -61,6 +63,21 @@ export function detectSync(options: DetectOptions = {}): DetectResult | null {
61
63
return null
62
64
}
63
65
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
+
64
81
function * lookup ( cwd : string = process . cwd ( ) ) : Generator < string > {
65
82
let directory = path . resolve ( cwd )
66
83
const { root } = path . parse ( directory )
0 commit comments