-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,34 @@ | ||
// Simple version of `find-project-root` | ||
// https://github.com/kirstein/find-project-root/blob/master/index.js | ||
|
||
import path from "node:path"; | ||
|
||
import iterateDirectoryUp from "iterate-directory-up"; | ||
import * as path from "node:path"; | ||
|
||
import isDirectory from "../utils/is-directory.js"; | ||
import Searcher from "./searcher.js"; | ||
|
||
const MARKERS = [".git", ".hg"]; | ||
let searcher; | ||
const searchOptions = { | ||
names: MARKERS, | ||
filter: ({ path: directory }) => | ||
isDirectory(directory, { allowSymlinks: false }), | ||
}; | ||
|
||
/** | ||
* Find the directory contains a version control system directory | ||
* @param {string} startDirectory | ||
* @param {{shouldCache?: boolean}} options | ||
* @returns {Promise<string | undefined>} | ||
*/ | ||
async function findProjectRoot(startDirectory) { | ||
for (const directory of iterateDirectoryUp(startDirectory)) { | ||
for (const name of MARKERS) { | ||
const directoryPath = path.join(directory, name); | ||
async function findProjectRoot(startDirectory, options) { | ||
searcher ??= new Searcher(searchOptions); | ||
const mark = await searcher.search(startDirectory, options); | ||
|
||
return mark ? path.dirname(mark) : undefined; | ||
} | ||
|
||
if (await isDirectory(directoryPath, { allowSymlinks: false })) { | ||
return directory; | ||
} | ||
} | ||
} | ||
function clearFindProjectRootCache() { | ||
searcher?.clearCache(); | ||
} | ||
|
||
export default findProjectRoot; | ||
export { clearFindProjectRootCache, findProjectRoot }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import path from "node:path"; | ||
|
||
import iterateDirectoryUp from "iterate-directory-up"; | ||
|
||
class Searcher { | ||
#names; | ||
#filter; | ||
#stopDirectory; | ||
#cache = new Map(); | ||
|
||
/** | ||
* @param {{ | ||
* names: string[], | ||
* filter: (fileOrDirectory: {name: string, path: string}) => Promise<boolean>, | ||
* stopDirectory?: string, | ||
* }} param0 | ||
*/ | ||
constructor({ names, filter, stopDirectory }) { | ||
this.#names = names; | ||
this.#filter = filter; | ||
this.#stopDirectory = stopDirectory; | ||
} | ||
|
||
async #searchInDirectory(directory, shouldCache) { | ||
const cache = this.#cache; | ||
if (shouldCache && cache.has(directory)) { | ||
return cache.get(directory); | ||
} | ||
|
||
for (const name of this.#names) { | ||
const fileOrDirectory = path.join(directory, name); | ||
|
||
if (await this.#filter({ name, path: fileOrDirectory })) { | ||
return fileOrDirectory; | ||
} | ||
} | ||
} | ||
|
||
async search(startDirectory, { shouldCache }) { | ||
const cache = this.#cache; | ||
if (shouldCache && cache.has(startDirectory)) { | ||
return cache.get(startDirectory); | ||
} | ||
|
||
const searchedDirectories = []; | ||
let result; | ||
for (const directory of iterateDirectoryUp( | ||
startDirectory, | ||
this.#stopDirectory, | ||
)) { | ||
searchedDirectories.push(directory); | ||
result = await this.#searchInDirectory(directory, shouldCache); | ||
|
||
if (result) { | ||
break; | ||
} | ||
} | ||
|
||
// Always cache the result, so we can use it when `useCache` is set to true | ||
for (const directory of searchedDirectories) { | ||
cache.set(directory, result); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
clearCache() { | ||
this.#cache.clear(); | ||
} | ||
} | ||
|
||
export default Searcher; |