Skip to content

Commit af58096

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

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ 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+
} else {
57+
rootDirname = parentDirname;
58+
}
5059
}
5160
return createResolver(rootDirname);
5261
};

nodekit/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gaubee/nodekit",
3-
"version": "0.10.1",
3+
"version": "0.10.2",
44
"exports": {
55
"./colors": "./src/colors.ts",
66
"./config_file": "./src/config_file.ts",

0 commit comments

Comments
 (0)