Skip to content

Commit

Permalink
fix: check for all lockfiles before ascending directories (#179)
Browse files Browse the repository at this point in the history
* chore: add .DS_Store to .gitignore

* fix: resolve lockfile

* chore: small tweak

---------

Co-authored-by: Daniel Roe <daniel@roe.dev>
  • Loading branch information
werheng and danielroe committed May 9, 2024
1 parent fe5b54e commit b24c863
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
*.tmp
.nyc*
coverage
.DS_Store
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,24 @@ const lockFiles = [
];

/**
* Resolves the path to the nearest `tsconfig.json` file from a given directory.
* Resolves the path to the nearest lockfile from a given directory.
* @param id - The base path for the search, defaults to the current working directory.
* @param options - Options to modify the search behaviour. See {@link ResolveOptions}.
* @returns A promise resolving to the path of the nearest `tsconfig.json` file.
* @returns A promise resolving to the path of the nearest lockfile.
*/
export async function resolveLockfile(
id: string = process.cwd(),
options: ResolveOptions = {},
): Promise<string> {
const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, options);
const _options = { startingFrom: resolvedPath, ...options };
for (const lockFile of lockFiles) {
try {
return await findNearestFile(lockFile, _options);
} catch {
// Ignore
}

try {
return await findNearestFile(lockFiles, _options);
} catch {
// Ignore
}

throw new Error("No lockfile found from " + id);
}

Expand Down
21 changes: 13 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ const defaultFindOptions: Required<FindFileOptions> = {
* @throws Will throw an error if the file cannot be found.
*/
export async function findFile(
filename: string,
filename: string | string[],
_options: FindFileOptions = {},
): Promise<string> {
const filenames = Array.isArray(filename) ? filename : [filename];
const options = { ...defaultFindOptions, ..._options };
const basePath = resolve(options.startingFrom);
const leadingSlash = basePath[0] === "/";
Expand All @@ -74,16 +75,20 @@ export async function findFile(

if (options.reverse) {
for (let index = root + 1; index <= segments.length; index++) {
const filePath = join(...segments.slice(0, index), filename);
if (await options.test(filePath)) {
return filePath;
for (const filename of filenames) {
const filePath = join(...segments.slice(0, index), filename);
if (await options.test(filePath)) {
return filePath;
}
}
}
} else {
for (let index = segments.length; index > root; index--) {
const filePath = join(...segments.slice(0, index), filename);
if (await options.test(filePath)) {
return filePath;
for (const filename of filenames) {
const filePath = join(...segments.slice(0, index), filename);
if (await options.test(filePath)) {
return filePath;
}
}
}
}
Expand All @@ -101,7 +106,7 @@ export async function findFile(
* @returns A promise that resolves to the path of the next file found.
*/
export function findNearestFile(
filename: string,
filename: string | string[],
_options: FindFileOptions = {},
): Promise<string> {
return findFile(filename, _options);
Expand Down

0 comments on commit b24c863

Please sign in to comment.