Skip to content

Commit

Permalink
feat(core): support findUp with includeDir option (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jan 16, 2024
1 parent 29caff6 commit 25e1d0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-hotels-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pkgr/core": patch
---

feat: support findUp with `includeDir` option
15 changes: 13 additions & 2 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export const tryExtensions = (filepath: string, extensions = EXTENSIONS) => {
return ext == null ? '' : filepath + ext
}

export const findUp = (searchEntry: string, searchFile = 'package.json') => {
export const findUp = (
searchEntry: string,
searchFileOrIncludeDir?: boolean | string,
includeDir?: boolean,
) => {
console.assert(path.isAbsolute(searchEntry))

if (
Expand All @@ -49,8 +53,15 @@ export const findUp = (searchEntry: string, searchFile = 'package.json') => {
: path.resolve(searchEntry, '..'),
)

const isSearchFile = typeof searchFileOrIncludeDir === 'string'

const searchFile = isSearchFile ? searchFileOrIncludeDir : 'package.json'

do {
const searched = tryFile(path.resolve(searchEntry, searchFile))
const searched = tryFile(
path.resolve(searchEntry, searchFile),
isSearchFile && includeDir,
)
if (searched) {
return searched
}
Expand Down

0 comments on commit 25e1d0e

Please sign in to comment.