Skip to content

Commit

Permalink
✨ 新增了createProjectResolver 工具函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaubee committed Feb 16, 2025
1 parent 91b97b6 commit 19ce6cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nodekit/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gaubee/nodekit",
"version": "0.3.4",
"version": "0.5.0",
"exports": {
"./config_file": "./src/config_file.ts",
"./fs": "./src/fs.ts",
Expand Down
16 changes: 14 additions & 2 deletions nodekit/src/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import node_path from "node:path";
import fs from "node:fs";
import process from "node:process";
import { fileURLToPath } from "node:url";
/**
Expand All @@ -22,12 +23,23 @@ type PathResolver = (...paths: string[]) => string;
* 创建一个 path.resolve 柯里化函数
*/
export const createResolver = (cwd: string): PathResolver => {
return (...paths: string[]) => {
return Object.assign((...paths: string[]) => {
return normalizeFilePath(node_path.resolve(cwd, ...paths));
};
}, { dirname: cwd });
};

/**
* 等同于 path.resolve(process.cwd(), ...paths)
*/
export const resolveCwd: PathResolver = createResolver(process.cwd());

/** 寻找一个包文件目录 */
export const createProjectResolver = (fromPath: string | URL = process.cwd(), projectRootFilename = "package.json") => {
let rootDirname = normalizeFilePath(fromPath);
while (
false === fs.existsSync(node_path.resolve(rootDirname, projectRootFilename))
) {
rootDirname = node_path.resolve(rootDirname, "..");
}
return createResolver(rootDirname);
};

0 comments on commit 19ce6cd

Please sign in to comment.