Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ async function parsePackageJson(
* @returns {Promise<DetectResult | null>} The detected package manager or `null` if not found.
*/
export async function detect(options: DetectOptions = {}): Promise<DetectResult | null> {
const { cwd, strategies = ['lockfile', 'packageManager-field', 'devEngines-field'], onUnknown } = options
const { cwd, strategies = ['lockfile', 'packageManager-field', 'devEngines-field'], onUnknown, stop } = options
const resolvedStop = stop ? path.resolve(stop) : undefined

for (const directory of lookup(cwd)) {
for (const strategy of strategies) {
Expand Down Expand Up @@ -98,6 +99,10 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
}
}
}

// Stop the traversing if the stop directory is reached
if (resolvedStop && directory === resolvedStop)
break
}

return null
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export interface DetectOptions {
* @param packageManager - The `packageManager` value from package.json file.
*/
onUnknown?: (packageManager: string) => DetectResult | null | undefined
/**
* The path to stop traversing up the directory.
*/
stop?: string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps a discussion point for others if we should consider stopDir instead (used by lilconfig)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would this be better as a function that takes each dir and call it shouldStop(dir: string)? A static path can have many edge cases of path normalization, tailing slash, etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe it should be safe since path.resolve(stop) was called so it normalizes it. We could reference how lilconfig handles it though, but I think a string is easier to understand and configure.

}

export interface DetectResult {
Expand Down
32 changes: 32 additions & 0 deletions test/detect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,35 @@ function getFixtureDirs(fixture: string) {
const items = fs.readdirSync(fixtureDir)
return items.filter(item => fs.statSync(path.join(fixtureDir, item)).isDirectory())
}

it('stops at specified directory', async () => {
const cwd = await fs.mkdtemp(path.join(tmpdir(), 'ni-'))

const noFilesDir = path.join(cwd, 'no-files')
const nestedNoFilesDir = path.join(noFilesDir, 'nested')
const parentDir = cwd

await fs.mkdirp(noFilesDir)
await fs.mkdirp(nestedNoFilesDir)

await fs.copy(
path.join(__dirname, 'fixtures', 'lockfile', 'npm'),
parentDir,
)

const resultWithStop = await detect({
cwd: nestedNoFilesDir,
stop: noFilesDir,
})

expect(resultWithStop).toBe(null)

const resultWithoutStop = await detect({
cwd: nestedNoFilesDir,
})

expect(resultWithoutStop).toMatchObject({
name: 'npm',
agent: 'npm',
})
})