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

fix: check for all lockfiles before ascending directories #179

Merged
merged 3 commits into from
May 9, 2024
Merged
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
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 @@
];

/**
* 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

Check warning on line 182 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L178-L182

Added lines #L178 - L182 were not covered by tests
}

Check warning on line 184 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L184

Added line #L184 was not covered by tests
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