Skip to content

Commit 1ca6ee9

Browse files
committed
feat: monorepoOnly option
1 parent d4cdf35 commit 1ca6ee9

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export const AGENTS: Agent[] = [
99
'bun',
1010
]
1111

12+
// the order here matters, more specific one comes first
13+
export const WORKSPACE_DEFS: Record<string, AgentName> = {
14+
'pnpm-lock.yaml': 'pnpm',
15+
}
16+
1217
// the order here matters, more specific one comes first
1318
export const LOCKS: Record<string, AgentName> = {
1419
'bun.lockb': 'bun',

src/detect.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fsPromises from 'node:fs/promises'
33
import path from 'node:path'
44
import process from 'node:process'
55
import type { Agent, AgentName, DetectOptions, DetectResult } from './types'
6-
import { AGENTS, LOCKS } from './constants'
6+
import { AGENTS, LOCKS, WORKSPACE_DEFS } from './constants'
77

88
/**
99
* Detects the package manager used in the project.
@@ -15,21 +15,28 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
1515

1616
for (const directory of lookup(cwd)) {
1717
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]
2222
const result = getFromPackageManagerField(pkg, onUnknown)
23-
if (result)
24-
return result
25-
else
26-
return { name, agent: name }
23+
return result ?? { name, agent: name }
2724
}
2825
}
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+
}
3340
}
3441

3542
return null
@@ -45,21 +52,28 @@ export function detectSync(options: DetectOptions = {}): DetectResult | null {
4552

4653
for (const directory of lookup(cwd)) {
4754
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)) {
5057
if (fileExistsSync(path.join(directory, lock))) {
51-
const name = LOCKS[lock]
58+
const name = WORKSPACE_DEFS[lock]
5259
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+
}
5771
}
72+
// Look in package.json
73+
const result = getFromPackageManagerField(pkg, onUnknown)
74+
if (result)
75+
return result
5876
}
59-
// Look in package.json
60-
const result = getFromPackageManagerField(pkg, onUnknown)
61-
if (result)
62-
return result
6377
}
6478

6579
return null
@@ -99,7 +113,7 @@ async function parsePackageJson(filepath: string): Promise<any> {
99113
}
100114

101115
function parsePackageJsonSync(filepath: string): any | null {
102-
if (!filepath || !fileExists(filepath)) {
116+
if (!filepath || !fileExistsSync(filepath)) {
103117
return null
104118
}
105119
return JSON.parse(fs.readFileSync(filepath, 'utf8'))

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface DetectOptions {
3737
* @default `process.cwd()`
3838
*/
3939
cwd?: string
40+
/**
41+
* Only detect the package manager if within a monorepo
42+
*/
43+
monorepoOnly?: boolean
4044
/**
4145
* Callback when unknown package manager from package.json.
4246
* @param packageManager - The `packageManager` value from package.json file.

0 commit comments

Comments
 (0)