Skip to content

Commit 9108b90

Browse files
committed
🐛 修复createResolverByRootFile死循环
1 parent ee75830 commit 9108b90

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

node/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gaubee/node",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"exports": {
55
"./env": "./src/env.ts",
66
"./path": "./src/path.ts",

node/src/path.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,17 @@ export const cwdResolver: PathResolver = /*@__PURE__*/ createResolver(process.cw
4343
/**
4444
* 向上寻找某一个文件名,以它所在的目录创建 resolver
4545
*/
46-
export const createResolverByRootFile = (fromPath: string | URL = process.cwd(), rootFilename = "package.json"): PathResolver => {
46+
export const createResolverByRootFile = (fromPath: string | URL = process.cwd(), rootFilename = "package.json", else_fn?: () => string): PathResolver => {
4747
let rootDirname = normalizeFilePath(fromPath);
4848
while (false === fs.existsSync(node_path.resolve(rootDirname, rootFilename))) {
49-
rootDirname = node_path.resolve(rootDirname, "..");
49+
const parentDirname = node_path.dirname(rootDirname);
50+
if (parentDirname === rootDirname) {
51+
const customDirname = else_fn?.();
52+
if (null == customDirname) {
53+
throw new Error(`Cannot find ${rootFilename} from ${fromPath}`);
54+
}
55+
rootDirname = customDirname;
56+
}
5057
}
5158
return createResolver(rootDirname);
5259
};

0 commit comments

Comments
 (0)