Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: monorepoOnly option #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
93 changes: 54 additions & 39 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 @@ -14,21 +14,29 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
const { cwd, onUnknown } = options

for (const directory of lookup(cwd)) {
// Look up for lock files
for (const lock of Object.keys(LOCKS)) {
if (await fileExists(path.join(directory, lock))) {
const name = LOCKS[lock]
const result = await parsePackageJson(path.join(directory, 'package.json'), onUnknown)
if (result)
return result
else
return { name, agent: name }
const pkg = await parsePackageJson(path.join(directory, 'package.json'))
// 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)
return result ?? { name, agent: name }
}
}
// Look up for package.json
const result = await parsePackageJson(path.join(directory, 'package.json'), 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 @@ -43,21 +51,29 @@ export function detectSync(options: DetectOptions = {}): DetectResult | null {
const { cwd, onUnknown } = options

for (const directory of lookup(cwd)) {
// Look up for lock files
for (const lock of Object.keys(LOCKS)) {
const pkg = parsePackageJsonSync(path.join(directory, 'package.json'))
// Look for workspace definitions
for (const lock of Object.keys(WORKSPACE_DEFS)) {
if (fileExistsSync(path.join(directory, lock))) {
const name = LOCKS[lock]
const result = parsePackageJsonSync(path.join(directory, 'package.json'), onUnknown)
if (result)
return result
else
return { name, agent: name }
const name = WORKSPACE_DEFS[lock]
const result = getFromPackageManagerField(pkg, onUnknown)
return result ?? { name, agent: name }
}
}
// Look up for package.json
const result = parsePackageJsonSync(path.join(directory, 'package.json'), onUnknown)
if (result)
return result
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
}
}

return null
Expand Down Expand Up @@ -89,27 +105,26 @@ function * lookup(cwd: string = process.cwd()): Generator<string> {
}
}

async function parsePackageJson(
filepath: string,
onUnknown: DetectOptions['onUnknown'],
): Promise<DetectResult | null> {
return !filepath || !await fileExists(filepath) ? null : handlePackageManager(filepath, onUnknown)
async function parsePackageJson(filepath: string): Promise<any> {
if (!filepath || !await fileExists(filepath)) {
return null
}
return JSON.parse(await fsPromises.readFile(filepath, 'utf8'))
}

function parsePackageJsonSync(
filepath: string,
onUnknown: DetectOptions['onUnknown'],
): DetectResult | null {
return !filepath || !fileExistsSync(filepath) ? null : handlePackageManager(filepath, onUnknown)
function parsePackageJsonSync(filepath: string): any | null {
if (!filepath || !fileExistsSync(filepath)) {
return null
}
return JSON.parse(fs.readFileSync(filepath, 'utf8'))
}

function handlePackageManager(
filepath: string,
function getFromPackageManagerField(
pkg: any,
onUnknown: DetectOptions['onUnknown'],
) {
// read `packageManager` field in package.json
try {
const pkg = JSON.parse(fs.readFileSync(filepath, 'utf8'))
let agent: Agent | undefined
if (typeof pkg.packageManager === 'string') {
const [name, ver] = pkg.packageManager.replace(/^\^/, '').split('@')
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it reads a bit confusing. Do you mean at monorepo root, or just inside a monorepo? And that would be the expected behaviour when it's not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean just inside a monorepo

The issue I'm trying to solve here is that we've gotten several bug reports when setting up new Svelte projects where people unknowingly have a package.lock or yarn.lock in a parent directory and it detects those files and chooses the package manager based off of them, but it's not actually what the user wants. If we're setting up a new project within a monorepo then we want to pay attention to what's there, but otherwise we want to ignore it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead you could provide a rootDir that we stop looking for lock files up?

people unknowingly have a package.lock or yarn.lock in a parent directory

Which to me, it seems to be a mistake they should fix on their side. Or is there any valid usage of that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead you could provide a rootDir that we stop looking for lock files up?

We wouldn't know the rootDir as it's the user's project. We would copy the code from this PR to find the root dir. Or we could change this PR to provide a utility method to find whether there's a monorepo root

Which to me, it seems to be a mistake they should fix on their side. Or is there any valid usage of that?

It is a mistake. But one that happens with annoying frequency and causes us to get bug reports I'd rather avoid

/**
* Callback when unknown package manager from package.json.
* @param packageManager - The `packageManager` value from package.json file.
Expand Down