@@ -3,7 +3,7 @@ import fsPromises from 'node:fs/promises'
3
3
import path from 'node:path'
4
4
import process from 'node:process'
5
5
import type { Agent , AgentName , DetectOptions , DetectResult } from './types'
6
- import { AGENTS , LOCKS } from './constants'
6
+ import { AGENTS , LOCKS , WORKSPACE_DEFS } from './constants'
7
7
8
8
/**
9
9
* Detects the package manager used in the project.
@@ -15,21 +15,28 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
15
15
16
16
for ( const directory of lookup ( cwd ) ) {
17
17
const pkg = await parsePackageJson ( path . join ( directory , 'package.json' ) )
18
- // Look for lock files
19
- for ( const lock of Object . keys ( LOCKS ) ) {
20
- if ( await fileExists ( path . join ( directory , lock ) ) ) {
21
- const name = LOCKS [ lock ]
18
+ // Look for workspace definitions
19
+ for ( const file of Object . keys ( WORKSPACE_DEFS ) ) {
20
+ if ( await fileExists ( path . join ( directory , file ) ) ) {
21
+ const name = WORKSPACE_DEFS [ file ]
22
22
const result = getFromPackageManagerField ( pkg , onUnknown )
23
- if ( result )
24
- return result
25
- else
26
- return { name, agent : name }
23
+ return result ?? { name, agent : name }
27
24
}
28
25
}
29
- // Look in package.json
30
- const result = getFromPackageManagerField ( pkg , onUnknown )
31
- if ( result )
32
- return result
26
+ if ( ! options . monorepoOnly || pkg . workspaces ) {
27
+ // Look for lock files
28
+ for ( const lock of Object . keys ( LOCKS ) ) {
29
+ if ( await fileExists ( path . join ( directory , lock ) ) ) {
30
+ const name = LOCKS [ lock ]
31
+ const result = getFromPackageManagerField ( pkg , onUnknown )
32
+ return result ?? { name, agent : name }
33
+ }
34
+ }
35
+ // Look in package.json
36
+ const result = getFromPackageManagerField ( pkg , onUnknown )
37
+ if ( result )
38
+ return result
39
+ }
33
40
}
34
41
35
42
return null
@@ -45,21 +52,28 @@ export function detectSync(options: DetectOptions = {}): DetectResult | null {
45
52
46
53
for ( const directory of lookup ( cwd ) ) {
47
54
const pkg = parsePackageJsonSync ( path . join ( directory , 'package.json' ) )
48
- // Look for lock files
49
- for ( const lock of Object . keys ( LOCKS ) ) {
55
+ // Look for workspace definitions
56
+ for ( const lock of Object . keys ( WORKSPACE_DEFS ) ) {
50
57
if ( fileExistsSync ( path . join ( directory , lock ) ) ) {
51
- const name = LOCKS [ lock ]
58
+ const name = WORKSPACE_DEFS [ lock ]
52
59
const result = getFromPackageManagerField ( pkg , onUnknown )
53
- if ( result )
54
- return result
55
- else
56
- return { name, agent : name }
60
+ return result ?? { name, agent : name }
61
+ }
62
+ }
63
+ if ( ! options . monorepoOnly || pkg . workspaces ) {
64
+ // Look for lock files
65
+ for ( const lock of Object . keys ( LOCKS ) ) {
66
+ if ( fileExistsSync ( path . join ( directory , lock ) ) ) {
67
+ const name = LOCKS [ lock ]
68
+ const result = getFromPackageManagerField ( pkg , onUnknown )
69
+ return result ?? { name, agent : name }
70
+ }
57
71
}
72
+ // Look in package.json
73
+ const result = getFromPackageManagerField ( pkg , onUnknown )
74
+ if ( result )
75
+ return result
58
76
}
59
- // Look in package.json
60
- const result = getFromPackageManagerField ( pkg , onUnknown )
61
- if ( result )
62
- return result
63
77
}
64
78
65
79
return null
@@ -99,7 +113,7 @@ async function parsePackageJson(filepath: string): Promise<any> {
99
113
}
100
114
101
115
function parsePackageJsonSync ( filepath : string ) : any | null {
102
- if ( ! filepath || ! fileExists ( filepath ) ) {
116
+ if ( ! filepath || ! fileExistsSync ( filepath ) ) {
103
117
return null
104
118
}
105
119
return JSON . parse ( fs . readFileSync ( filepath , 'utf8' ) )
0 commit comments