@ruan-cat/utils 4.16.0 → 4.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +14 -1
- package/dist/index.js +2632 -0
- package/dist/index.js.map +1 -1
- package/dist/node-cjs/index.cjs +2633 -0
- package/dist/node-cjs/index.cjs.map +1 -1
- package/dist/node-cjs/index.d.cts +14 -1
- package/dist/node-esm/index.js +57 -49
- package/dist/node-esm/index.js.map +1 -1
- package/package.json +9 -8
- package/src/index.ts +1 -0
- package/src/monorepo.ts +70 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.18.0",
|
|
4
4
|
"description": "阮喵喵工具集合。默认提供js文件,也直接提供ts文件。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -65,15 +65,16 @@
|
|
|
65
65
|
],
|
|
66
66
|
"dependencies": {
|
|
67
67
|
"@vueuse/integrations": "^13.9.0",
|
|
68
|
-
"axios": "^1.
|
|
68
|
+
"axios": "^1.13.2",
|
|
69
69
|
"consola": "^3.4.2",
|
|
70
|
+
"tinyglobby": "^0.2.15",
|
|
70
71
|
"lodash-es": "^4.17.21"
|
|
71
72
|
},
|
|
72
73
|
"devDependencies": {
|
|
73
|
-
"@antfu/utils": "^9.
|
|
74
|
+
"@antfu/utils": "^9.3.0",
|
|
74
75
|
"@types/js-yaml": "^4.0.9",
|
|
75
76
|
"@types/lodash-es": "^4.17.12",
|
|
76
|
-
"@types/node": "^22.
|
|
77
|
+
"@types/node": "^22.19.0",
|
|
77
78
|
"@types/qs": "^6.14.0",
|
|
78
79
|
"automd": "^0.4.2",
|
|
79
80
|
"commander": "^13.1.0",
|
|
@@ -81,12 +82,12 @@
|
|
|
81
82
|
"qs": "^6.14.0",
|
|
82
83
|
"tsup": "^8.5.0",
|
|
83
84
|
"type-plus": "^7.6.2",
|
|
84
|
-
"typedoc": "^0.28.
|
|
85
|
+
"typedoc": "^0.28.14",
|
|
85
86
|
"typedoc-plugin-frontmatter": "^1.3.0",
|
|
86
87
|
"typedoc-plugin-markdown": "^4.9.0",
|
|
87
|
-
"typescript": "^5.9.
|
|
88
|
-
"unplugin-auto-import": "^20.
|
|
89
|
-
"unplugin-vue-router": "^0.
|
|
88
|
+
"typescript": "^5.9.3",
|
|
89
|
+
"unplugin-auto-import": "^20.2.0",
|
|
90
|
+
"unplugin-vue-router": "^0.16.1",
|
|
90
91
|
"vite-plugin-autogeneration-import-file": "^3.0.0",
|
|
91
92
|
"vitepress": "^1.6.4"
|
|
92
93
|
},
|
package/src/index.ts
CHANGED
package/src/monorepo.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import { globSync } from "tinyglobby";
|
|
4
|
+
import { load } from "js-yaml";
|
|
5
|
+
import { isUndefined } from "lodash-es";
|
|
6
|
+
import type { PnpmWorkspace } from "./types/pnpm-workspace.yaml.shim";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 路径转换工具
|
|
10
|
+
* @private 暂时不考虑复用
|
|
11
|
+
*/
|
|
12
|
+
function pathChange(path: string) {
|
|
13
|
+
return path.replace(/\\/g, "/");
|
|
14
|
+
// FIXME: 无法有效地实现解析路径 测试用例不通过
|
|
15
|
+
// return normalize(path);
|
|
16
|
+
// FIXME: tsup打包时,无法处理好vite的依赖 会导致打包失败 不知道怎么单独使用并打包该函数
|
|
17
|
+
// return normalizePath(path);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 判断目标项目是否是 monorepo 格式的项目
|
|
22
|
+
* @description
|
|
23
|
+
* 判别逻辑:
|
|
24
|
+
* 1. 目标项目同时存在 `pnpm-workspace.yaml` 文件
|
|
25
|
+
* 2. `pnpm-workspace.yaml` 提供了有效的 packages 匹配配置
|
|
26
|
+
* 3. 至少能匹配到一个 package.json 文件
|
|
27
|
+
*
|
|
28
|
+
* @returns {boolean} 是否是 monorepo 项目
|
|
29
|
+
* @throws {Error} 当 pnpm-workspace.yaml 格式错误时抛出错误
|
|
30
|
+
*/
|
|
31
|
+
export function isMonorepoProject(): boolean {
|
|
32
|
+
// 1. 检查 pnpm-workspace.yaml 是否存在
|
|
33
|
+
const workspaceConfigPath = join(process.cwd(), "pnpm-workspace.yaml");
|
|
34
|
+
if (!fs.existsSync(workspaceConfigPath)) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 2. 解析 YAML 文件
|
|
39
|
+
let workspaceConfig: PnpmWorkspace;
|
|
40
|
+
try {
|
|
41
|
+
const workspaceFile = fs.readFileSync(workspaceConfigPath, "utf8");
|
|
42
|
+
workspaceConfig = <PnpmWorkspace>load(workspaceFile);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`解析 pnpm-workspace.yaml 文件失败,请检查文件格式是否正确。错误信息:${error instanceof Error ? error.message : String(error)}`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 3. 检查 packages 配置是否存在且不为空数组
|
|
50
|
+
const pkgPatterns = workspaceConfig.packages;
|
|
51
|
+
if (isUndefined(pkgPatterns) || pkgPatterns.length === 0) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 4. 检查是否至少能匹配到一个 package.json
|
|
56
|
+
for (const pkgPattern of pkgPatterns) {
|
|
57
|
+
const matchedPath = pathChange(join(process.cwd(), pkgPattern, "package.json"));
|
|
58
|
+
const matchedPaths = globSync(matchedPath, {
|
|
59
|
+
ignore: ["**/node_modules/**"],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// 只要有一个模式能匹配到文件,就认为是有效的 monorepo
|
|
63
|
+
if (matchedPaths.length > 0) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 没有匹配到任何 package.json
|
|
69
|
+
return false;
|
|
70
|
+
}
|