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: add stop option to detect #39

Open
wants to merge 1 commit 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
12 changes: 10 additions & 2 deletions src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AGENTS, LOCKS } from './constants'
* @returns {Promise<DetectResult | null>} The detected package manager or `null` if not found.
*/
export async function detect(options: DetectOptions = {}): Promise<DetectResult | null> {
const { cwd, onUnknown } = options
const { cwd, onUnknown, stop } = options

for (const directory of lookup(cwd)) {
// Look up for lock files
Expand All @@ -29,6 +29,10 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
const result = await parsePackageJson(path.join(directory, 'package.json'), onUnknown)
if (result)
return result

// Stop the traversing if the stop directory is reached
if (stop && path.resolve(directory) === path.resolve(stop))
break
}

return null
Expand All @@ -40,7 +44,7 @@ export async function detect(options: DetectOptions = {}): Promise<DetectResult
* @returns {DetectResult | null>} The detected package manager or `null` if not found.
*/
export function detectSync(options: DetectOptions = {}): DetectResult | null {
const { cwd, onUnknown } = options
const { cwd, onUnknown, stop } = options

for (const directory of lookup(cwd)) {
// Look up for lock files
Expand All @@ -58,6 +62,10 @@ export function detectSync(options: DetectOptions = {}): DetectResult | null {
const result = parsePackageJsonSync(path.join(directory, 'package.json'), onUnknown)
if (result)
return result

// Stop the traversing if the stop directory is reached
if (stop && path.resolve(directory) === path.resolve(stop))
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 @@ -42,6 +42,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
}

export interface DetectResult {
Expand Down
32 changes: 32 additions & 0 deletions test/detect-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,35 @@ fixtures.forEach(fixture => describe(fixture, () => agents.forEach((agent) => {
expect(infoLog).not.toHaveBeenCalled()
})
})))

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 = detectSync({
cwd: nestedNoFilesDir,
stop: noFilesDir,
})

expect(resultWithStop).toBe(null)

const resultWithoutStop = detectSync({
cwd: nestedNoFilesDir,
})

expect(resultWithoutStop).toMatchObject({
name: 'npm',
agent: 'npm',
})
})
32 changes: 32 additions & 0 deletions test/detect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,35 @@ fixtures.forEach(fixture => describe(fixture, () => agents.forEach((agent) => {
expect(infoLog).not.toHaveBeenCalled()
})
})))

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',
})
})