Skip to content

Commit

Permalink
remove find-up to remove six dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 7, 2024
1 parent aec5096 commit ae2d5b0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/find-root/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"module": "dist/manypkg-find-root.esm.js",
"dependencies": {
"@manypkg/tools": "^1.1.1",
"find-up": "^4.1.0",
"fs-extra": "^8.1.0"
},
"devDependencies": {
Expand Down
48 changes: 43 additions & 5 deletions packages/find-root/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import findUp, { sync as findUpSync } from "find-up";
import path from "path";
import fs from "fs-extra";
import fsp from "fs/promises";

import {
Tool,
Expand Down Expand Up @@ -98,7 +98,7 @@ export async function findRoot(
}
});
},
{ cwd, type: "directory" }
cwd
);

if (monorepoRoot) {
Expand All @@ -123,7 +123,7 @@ export async function findRoot(
}
}
},
{ cwd, type: "directory" }
cwd
);

if (!rootDir) {
Expand Down Expand Up @@ -158,7 +158,7 @@ export function findRootSync(
}
}
},
{ cwd, type: "directory" }
cwd
);

if (monorepoRoot) {
Expand All @@ -177,7 +177,7 @@ export function findRootSync(
const exists = fs.existsSync(path.join(directory, "package.json"));
return exists ? directory : undefined;
},
{ cwd, type: "directory" }
cwd
);

if (!rootDir) {
Expand All @@ -189,3 +189,41 @@ export function findRootSync(
rootDir,
};
}

export async function findUp(matcher: (directory: string) => Promise<string | undefined>, cwd = process.cwd()) {
let directory = path.resolve(cwd);
const { root } = path.parse(directory);

while (directory && directory !== root) {
const filePath = await matcher(directory);

if (filePath) {
return path.resolve(directory, filePath);
}

if (directory === root) {
return;
}

directory = path.dirname(directory);
}
}

export function findUpSync(matcher: (directory: string) => string | undefined, cwd = process.cwd()) {
let directory = path.resolve(cwd);
const { root } = path.parse(directory);

while (directory && directory !== root) {
const filePath = matcher(directory);

if (filePath) {
return path.resolve(directory, filePath);
}

if (directory === root) {
return;
}

directory = path.dirname(directory);
}
}

0 comments on commit ae2d5b0

Please sign in to comment.