Skip to content

Commit

Permalink
remove find-up to remove six dependencies (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Aug 9, 2024
1 parent bc7a4d8 commit 2ebda03
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-goats-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/find-root": patch
---

Remove find-up dependency
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
37 changes: 32 additions & 5 deletions packages/find-root/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import findUp, { sync as findUpSync } from "find-up";
import path from "path";
import fs from "fs-extra";

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

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

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

if (monorepoRoot) {
Expand All @@ -177,7 +176,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 +188,31 @@ export function findRootSync(
rootDir,
};
}

async function findUp(matcher: (directory: string) => Promise<string | undefined>, cwd: string) {
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);
}

directory = path.dirname(directory);
}
}

function findUpSync(matcher: (directory: string) => string | undefined, cwd: string) {
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);
}

directory = path.dirname(directory);
}
}

0 comments on commit 2ebda03

Please sign in to comment.