Skip to content

Commit

Permalink
feat: monorepoOnly option
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Oct 15, 2024
1 parent d4cdf35 commit 1ca6ee9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export const AGENTS: Agent[] = [
'bun',
]

// the order here matters, more specific one comes first
export const WORKSPACE_DEFS: Record<string, AgentName> = {
'pnpm-lock.yaml': 'pnpm',
}

// the order here matters, more specific one comes first
export const LOCKS: Record<string, AgentName> = {
'bun.lockb': 'bun',
Expand Down
64 changes: 39 additions & 25 deletions src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fsPromises from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import type { Agent, AgentName, DetectOptions, DetectResult } from './types'
import { AGENTS, LOCKS } from './constants'
import { AGENTS, LOCKS, WORKSPACE_DEFS } from './constants'

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

for (const directory of lookup(cwd)) {
const pkg = await parsePackageJson(path.join(directory, 'package.json'))
// Look for lock files
for (const lock of Object.keys(LOCKS)) {
if (await fileExists(path.join(directory, lock))) {
const name = LOCKS[lock]
// Look for workspace definitions
for (const file of Object.keys(WORKSPACE_DEFS)) {
if (await fileExists(path.join(directory, file))) {
const name = WORKSPACE_DEFS[file]
const result = getFromPackageManagerField(pkg, onUnknown)
if (result)
return result
else
return { name, agent: name }
return result ?? { name, agent: name }
}
}
// Look in package.json
const result = getFromPackageManagerField(pkg, onUnknown)
if (result)
return result
if (!options.monorepoOnly || pkg.workspaces) {
// Look for lock files
for (const lock of Object.keys(LOCKS)) {
if (await fileExists(path.join(directory, lock))) {
const name = LOCKS[lock]
const result = getFromPackageManagerField(pkg, onUnknown)
return result ?? { name, agent: name }
}
}
// Look in package.json
const result = getFromPackageManagerField(pkg, onUnknown)
if (result)
return result
}
}

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

for (const directory of lookup(cwd)) {
const pkg = parsePackageJsonSync(path.join(directory, 'package.json'))
// Look for lock files
for (const lock of Object.keys(LOCKS)) {
// Look for workspace definitions
for (const lock of Object.keys(WORKSPACE_DEFS)) {
if (fileExistsSync(path.join(directory, lock))) {
const name = LOCKS[lock]
const name = WORKSPACE_DEFS[lock]
const result = getFromPackageManagerField(pkg, onUnknown)
if (result)
return result
else
return { name, agent: name }
return result ?? { name, agent: name }
}
}
if (!options.monorepoOnly || pkg.workspaces) {
// Look for lock files
for (const lock of Object.keys(LOCKS)) {
if (fileExistsSync(path.join(directory, lock))) {
const name = LOCKS[lock]
const result = getFromPackageManagerField(pkg, onUnknown)
return result ?? { name, agent: name }
}
}
// Look in package.json
const result = getFromPackageManagerField(pkg, onUnknown)
if (result)
return result
}
// Look in package.json
const result = getFromPackageManagerField(pkg, onUnknown)
if (result)
return result
}

return null
Expand Down Expand Up @@ -99,7 +113,7 @@ async function parsePackageJson(filepath: string): Promise<any> {
}

function parsePackageJsonSync(filepath: string): any | null {
if (!filepath || !fileExists(filepath)) {
if (!filepath || !fileExistsSync(filepath)) {
return null
}
return JSON.parse(fs.readFileSync(filepath, 'utf8'))
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export interface DetectOptions {
* @default `process.cwd()`
*/
cwd?: string
/**
* Only detect the package manager if within a monorepo
*/
monorepoOnly?: boolean
/**
* Callback when unknown package manager from package.json.
* @param packageManager - The `packageManager` value from package.json file.
Expand Down

0 comments on commit 1ca6ee9

Please sign in to comment.