Skip to content

feat: add stop option to detect - #39

Merged
antfu merged 7 commits into
made-in-common:mainfrom
luxass:feat/detect-stop
May 4, 2025
Merged

antfu merged 7 commits into
made-in-common:mainfrom
luxass:feat/detect-stop

Conversation

@luxass

@luxass luxass commented Dec 31, 2024 •

Copy link
Copy Markdown
Contributor

Description

This PR adds a new stop option to detect. This will allow us to stop traversing directories when a certain directory is hit.

Linked Issues

N/A

Additional context

N/A

@benmccann

Copy link
Copy Markdown
Collaborator

What is the motivation for this?

@luxass

luxass commented Jan 1, 2025

Copy link
Copy Markdown
Contributor Author

What is the motivation for this?

I needed this feature for testing package manager detection in my codebase that uses pnpm. Specifically, I have tests that should return null when no package manager is found, but without the stop option, the traversal continues upward and finds the pnpm configuration at the repository root, causing the test to fail. The stop option allows me to properly test these scenarios by limiting how far up the directory tree the search goes.

@bluwy

bluwy commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

I personally would like to use this too for similar reasons, and plays in hand to JoshuaKGoldberg/formatly#54 (where it'd soon use package-manager-detector and would be nice to be able to pass on the option). @luxass are you able to rebase the PR?

Currently we can't break the traversing up directories, which we now can.
@luxass
luxass force-pushed the feat/detect-stop branch from 6676f55 to 189ac6c Compare April 18, 2025 09:34
Comment thread src/detect.ts Resolved
Comment thread src/types.ts Outdated
/**
* 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.

@bluwy bluwy left a comment

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.

LGTM 👍 I'm not a maintainer here, so will leave to them for the final review

@benmccann

Copy link
Copy Markdown
Collaborator

Seems like a reasonable idea. If we also exported a method to find the monorepo root so that users could provide that as the stop value then it could be a replacement for #23

@luxass luxass changed the title feat: add stop option to detect feat: add stop option to detect & export getWorkspaceRoot Apr 20, 2025
@luxass

luxass commented Apr 23, 2025

Copy link
Copy Markdown
Contributor Author

I'm wondering if there are any remaining concerns about this PR that I could address to help move it forward?

@bluwy

bluwy commented Apr 24, 2025

Copy link
Copy Markdown
Contributor

Looking back, I think I have a slight preference to renaming as stopDir. Otherwise we're waiting for the final call from @antfu here.

I personally don't find the need to do this now, but it would've made reviewing easier I imagine if the new workspace api is done in a separate PR.

Comment thread src/constants.ts Resolved
I will create a new separate issue, so we can decide whether or not to also detect lerna workspaces and other workspace tools.
@luxass luxass changed the title feat: add stop option to detect & export getWorkspaceRoot feat: add stop option to detect May 3, 2025
@antfu
antfu merged commit 6d8be92 into made-in-common:main May 4, 2025
@luxass
luxass deleted the feat/detect-stop branch May 4, 2025 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants