@ruan-cat/utils 4.11.0 → 4.12.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruan-cat/utils",
3
- "version": "4.11.0",
3
+ "version": "4.12.1",
4
4
  "description": "阮喵喵工具集合。默认提供js文件,也直接提供ts文件。",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -62,24 +62,24 @@
62
62
  ],
63
63
  "dependencies": {
64
64
  "@vueuse/integrations": "^13.9.0",
65
- "axios": "^1.12.0",
65
+ "axios": "^1.12.2",
66
66
  "consola": "^3.4.2",
67
67
  "lodash-es": "^4.17.21"
68
68
  },
69
69
  "devDependencies": {
70
- "@antfu/utils": "^9.2.0",
70
+ "@antfu/utils": "^9.2.1",
71
71
  "@types/lodash-es": "^4.17.12",
72
- "@types/node": "^22.18.1",
72
+ "@types/node": "^22.18.6",
73
73
  "@types/qs": "^6.14.0",
74
- "automd": "^0.4.0",
74
+ "automd": "^0.4.2",
75
75
  "commander": "^13.1.0",
76
76
  "js-yaml": "^4.1.0",
77
77
  "qs": "^6.14.0",
78
78
  "tsup": "^8.5.0",
79
79
  "type-plus": "^7.6.2",
80
- "typedoc": "^0.28.12",
80
+ "typedoc": "^0.28.13",
81
81
  "typedoc-plugin-frontmatter": "^1.3.0",
82
- "typedoc-plugin-markdown": "^4.8.1",
82
+ "typedoc-plugin-markdown": "^4.9.0",
83
83
  "typescript": "^5.9.2",
84
84
  "unplugin-auto-import": "^20.1.0",
85
85
  "unplugin-vue-router": "^0.15.0",
@@ -2,6 +2,7 @@ export * from "./ruan-cat-pkg-info";
2
2
 
3
3
  export * from "./scripts/clean";
4
4
  export * from "./scripts/copy-changelog";
5
+ export * from "./scripts/copy-claude-agents";
5
6
  export * from "./scripts/copy-readme";
6
7
  export * from "./scripts/yaml-to-md";
7
8
  export * from "./scripts/add-changelog-to-doc";
@@ -0,0 +1,154 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import consola from "consola";
4
+
5
+ /**
6
+ * 从当前工作目录向上查找 monorepo 根目录
7
+ * @returns monorepo 根目录的绝对路径,如果找不到则返回 null
8
+ * @description
9
+ * 通过查找 pnpm-workspace.yaml 文件来定位 monorepo 根目录。
10
+ * 从 process.cwd() 开始向上遍历,直到找到该文件或到达文件系统根目录。
11
+ */
12
+ function findMonorepoRoot(): string | null {
13
+ let currentDir = process.cwd();
14
+ const root = path.parse(currentDir).root;
15
+
16
+ while (currentDir !== root) {
17
+ const workspaceFile = path.join(currentDir, "pnpm-workspace.yaml");
18
+ if (fs.existsSync(workspaceFile)) {
19
+ return currentDir;
20
+ }
21
+ currentDir = path.dirname(currentDir);
22
+ }
23
+
24
+ // 检查根目录本身
25
+ const workspaceFile = path.join(root, "pnpm-workspace.yaml");
26
+ if (fs.existsSync(workspaceFile)) {
27
+ return root;
28
+ }
29
+
30
+ return null;
31
+ }
32
+
33
+ /**
34
+ * 解析根目录路径
35
+ * @param rootDir - 根目录路径,支持相对路径(如 `../../../`)或绝对路径
36
+ * @returns 解析后的绝对路径
37
+ * @description
38
+ * 路径解析优先级:
39
+ * 1. 如果传入 rootDir,将相对路径基于 process.cwd() 解析为绝对路径
40
+ * 2. 如果未传入 rootDir,自动向上查找包含 pnpm-workspace.yaml 的 monorepo 根目录
41
+ * 3. 如果自动查找失败,回退到 process.cwd()
42
+ */
43
+ function resolveRootDir(rootDir?: string): string {
44
+ if (rootDir) {
45
+ // 将相对路径或绝对路径解析为绝对路径
46
+ return path.resolve(process.cwd(), rootDir);
47
+ }
48
+
49
+ // 自动查找 monorepo 根目录
50
+ const monorepoRoot = findMonorepoRoot();
51
+ if (monorepoRoot) {
52
+ return monorepoRoot;
53
+ }
54
+
55
+ // 回退到当前工作目录
56
+ return process.cwd();
57
+ }
58
+
59
+ /**
60
+ * 检查指定根目录是否存在 .claude/agents 文件夹
61
+ * @param options - 配置选项
62
+ * @param options.rootDir - 可选的根目录路径,支持相对路径(如 `../../../` 表示向上三级目录)。
63
+ * 如果不传入,将自动向上查找包含 pnpm-workspace.yaml 的 monorepo 根目录。
64
+ * 相对路径会基于当前工作目录 (process.cwd()) 解析为绝对路径。
65
+ * @returns 是否存在 .claude/agents 文件夹
66
+ * @example
67
+ * // 自动检测 monorepo 根目录
68
+ * hasClaudeAgents()
69
+ *
70
+ * // 手动指定相对路径(从当前工作目录向上三级)
71
+ * hasClaudeAgents({ rootDir: '../../../' })
72
+ *
73
+ * // 手动指定绝对路径
74
+ * hasClaudeAgents({ rootDir: '/path/to/monorepo' })
75
+ */
76
+ export function hasClaudeAgents(options?: { rootDir?: string }): boolean {
77
+ const root = resolveRootDir(options?.rootDir);
78
+ const claudeAgentsPath = path.join(root, ".claude/agents");
79
+ const exists = fs.existsSync(claudeAgentsPath);
80
+
81
+ if (!exists) {
82
+ consola.log("检测的根目录为:", root);
83
+ consola.warn("该根目录不存在 .claude/agents 文件夹");
84
+ }
85
+
86
+ return exists;
87
+ }
88
+
89
+ /**
90
+ * 将 .claude/agents 文件夹复制到指定位置的配置选项
91
+ */
92
+ export interface CopyClaudeAgentsOptions {
93
+ /**
94
+ * 目标文件夹路径(相对于当前工作目录)
95
+ * @example 'dist', 'build/output', './public'
96
+ */
97
+ target: string;
98
+
99
+ /**
100
+ * 可选的根目录路径,支持相对路径(如 `../../../` 表示向上三级目录)
101
+ * @description
102
+ * - 如果不传入,将自动向上查找包含 pnpm-workspace.yaml 的 monorepo 根目录
103
+ * - 相对路径会基于当前工作目录 (process.cwd()) 解析为绝对路径
104
+ * - 绝对路径将直接使用
105
+ * @example
106
+ * // 相对路径:向上三级目录
107
+ * '../../../'
108
+ *
109
+ * // 绝对路径
110
+ * '/absolute/path/to/monorepo'
111
+ */
112
+ rootDir?: string;
113
+ }
114
+
115
+ /**
116
+ * 将 .claude/agents 文件夹复制到指定位置
117
+ * @param options - 配置选项
118
+ * @description
119
+ * 该函数相当于实现 `cpx .claude/agents <target>` 命令。
120
+ * 从根目录的 .claude/agents 复制到目标位置。
121
+ * @example
122
+ * // 自动检测 monorepo 根目录,复制到当前目录的 dist 文件夹
123
+ * copyClaudeAgents({ target: 'dist' })
124
+ *
125
+ * // 手动指定根目录为向上三级,复制到 build 文件夹
126
+ * copyClaudeAgents({
127
+ * target: 'build',
128
+ * rootDir: '../../../'
129
+ * })
130
+ *
131
+ * // 使用绝对路径指定根目录
132
+ * copyClaudeAgents({
133
+ * target: 'dist',
134
+ * rootDir: '/absolute/path/to/monorepo'
135
+ * })
136
+ */
137
+ export function copyClaudeAgents(options: CopyClaudeAgentsOptions): void {
138
+ // 检查源目录是否存在
139
+ if (!hasClaudeAgents({ rootDir: options.rootDir })) {
140
+ return;
141
+ }
142
+
143
+ const root = resolveRootDir(options.rootDir);
144
+ const source = path.join(root, ".claude/agents");
145
+ const destination = path.resolve(process.cwd(), options.target);
146
+
147
+ // 确保目标文件夹的父目录存在
148
+ fs.mkdirSync(path.dirname(destination), { recursive: true });
149
+
150
+ // 递归复制文件夹
151
+ fs.cpSync(source, destination, { recursive: true });
152
+
153
+ consola.success(`已成功复制 .claude/agents 到 ${destination}`);
154
+ }