@ruan-cat/utils 4.13.0 → 4.14.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruan-cat/utils",
3
- "version": "4.13.0",
3
+ "version": "4.14.0",
4
4
  "description": "阮喵喵工具集合。默认提供js文件,也直接提供ts文件。",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "exports": {
18
18
  ".": {
19
- "types": "./dsit/index.d.ts",
19
+ "types": "./dist/index.d.ts",
20
20
  "import": "./dist/index.js",
21
21
  "require": "./dist/index.cjs"
22
22
  },
@@ -1,6 +1,5 @@
1
1
  // 工具包文档项目 直接导入生成文档配置 避免出现循环依赖
2
- import { setUserConfig, setGenerateSidebar } from "../../../vitepress-preset-config";
3
- import { addChangelog2doc, copyReadmeMd } from "@ruan-cat/utils/node-esm";
2
+ import { setUserConfig, setGenerateSidebar, addChangelog2doc, copyReadmeMd } from "../../../vitepress-preset-config";
4
3
 
5
4
  import { description } from "../../package.json";
6
5
 
@@ -20,6 +19,9 @@ const userConfig = setUserConfig({
20
19
  title: "阮喵喵工具包",
21
20
  description,
22
21
  themeConfig: {
22
+ editLink: {
23
+ pattern: "https://github.com/ruan-cat/monorepo/blob/dev/packages/utils/src/:path",
24
+ },
23
25
  socialLinks: [
24
26
  {
25
27
  icon: "github",
@@ -1,8 +1,4 @@
1
1
  export * from "./ruan-cat-pkg-info";
2
2
 
3
3
  export * from "./scripts/clean";
4
- export * from "./scripts/copy-changelog";
5
- export * from "./scripts/copy-claude-agents";
6
- export * from "./scripts/copy-readme";
7
- export * from "./scripts/yaml-to-md";
8
- export * from "./scripts/add-changelog-to-doc";
4
+ export * from "./yaml-to-md";
@@ -4,19 +4,6 @@ import { consola } from "consola";
4
4
  import { isUndefined } from "lodash-es";
5
5
  import yaml from "js-yaml";
6
6
 
7
- // import { program } from "commander";
8
- // import prettier from "prettier";
9
- // import prettierConfig from "../prettier.config.js";
10
- // program
11
- // .name("yaml-in-md")
12
- // // 环境变量的地址
13
- // .option("--md <path>", "目标md文件的地址,目前仅考虑单个文件")
14
- // .parse();
15
- // const options = program.opts();
16
- // consola.info(" 查看命令行提供的参数 ", options);
17
- // /** md文件的地址 */
18
- // const defMdPath: string = options?.md;
19
-
20
7
  export interface WriteYaml2mdParams<T = Record<string, any>> {
21
8
  /** 目标md文件地址 */
22
9
  mdPath: string;
@@ -1,29 +0,0 @@
1
- import path from "node:path";
2
-
3
- import { copyChangelogMd, hasChangelogMd } from "./copy-changelog";
4
- import { writeYaml2md } from "./yaml-to-md";
5
-
6
- export interface AddChangelog2docOptions<T = Record<string, any>> {
7
- /** 目标文件夹 */
8
- target: string;
9
-
10
- /** 被插入到md头部的数据 */
11
- data: T;
12
- }
13
-
14
- /** 将变更日志添加到指定的文档目录内 并提供参数 */
15
- export function addChangelog2doc<T>(options: AddChangelog2docOptions<T>) {
16
- const { data, target } = options;
17
-
18
- if (!hasChangelogMd()) {
19
- return;
20
- }
21
-
22
- copyChangelogMd(target);
23
-
24
- const mdPath = path.resolve(process.cwd(), target, "CHANGELOG.md");
25
- writeYaml2md({
26
- mdPath,
27
- data,
28
- });
29
- }
@@ -1,29 +0,0 @@
1
- import fs from "node:fs";
2
- import path from "node:path";
3
- import consola from "consola";
4
-
5
- /** 检查当前运行的根目录 是否存在 CHANGELOG.md 文件 */
6
- export function hasChangelogMd() {
7
- const res = fs.existsSync(path.resolve(process.cwd(), "CHANGELOG.md"));
8
- if (!res) {
9
- consola.log("当前项目根目录为:", process.cwd());
10
- consola.warn("当前项目根目录不存在 CHANGELOG.md 文件");
11
- }
12
- return res;
13
- }
14
-
15
- /**
16
- * 将 CHANGELOG.md 文件移动到指定要求的位置内
17
- * @description
18
- * 该函数相当于实现 `cpx CHANGELOG.md docs` 命令
19
- */
20
- export function copyChangelogMd(/** 目标文件夹 */ target: string) {
21
- if (!hasChangelogMd()) {
22
- return;
23
- }
24
-
25
- const source = path.resolve(process.cwd(), "CHANGELOG.md");
26
- const destination = path.resolve(process.cwd(), target, "CHANGELOG.md");
27
-
28
- fs.copyFileSync(source, destination);
29
- }
@@ -1,194 +0,0 @@
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
- * @description
96
- * **重要**:此参数仅接受相对路径,不接受绝对路径(禁止以 `/` 或盘符如 `C:\` 开头)。
97
- * 使用绝对路径会抛出错误,这是为了防止意外覆盖系统目录。
98
- *
99
- * 该地址是写相对路径的 不能写绝对路径,容易导致意外。
100
- * vitepress 命令运行在 apps/admin 目录内,该地址是相对于该运行目录的。
101
- * 比如期望将 `.claude/agents` 复制到 `apps/admin/src/docs/prompts/agents` 文件夹。
102
- * 则写 `src/docs/prompts/agents` 即可。
103
- *
104
- * @throws {Error} 当传入绝对路径时抛出错误
105
- * @example
106
- * // ✅ 正确:相对路径
107
- * "src/docs/prompts/agents"
108
- * "dist/agents"
109
- * "./public/claude"
110
- *
111
- * @example
112
- * // ❌ 错误:绝对路径(会抛出错误)
113
- * "/var/www/agents" // Unix 绝对路径
114
- * "C:\\Users\\agents" // Windows 绝对路径
115
- */
116
- target: string;
117
-
118
- /**
119
- * 可选的根目录路径,支持相对路径(如 `../../../` 表示向上三级目录)
120
- * @description
121
- * - 如果不传入,将自动向上查找包含 pnpm-workspace.yaml 的 monorepo 根目录
122
- * - 相对路径会基于当前工作目录 (process.cwd()) 解析为绝对路径
123
- * - 绝对路径将直接使用
124
- * @example
125
- * // 相对路径:向上三级目录
126
- * '../../../'
127
- *
128
- * // 绝对路径
129
- * '/absolute/path/to/monorepo'
130
- */
131
- rootDir?: string;
132
- }
133
-
134
- /**
135
- * 将 .claude/agents 文件夹复制到指定位置
136
- * @param options - 配置选项
137
- * @throws {Error} 当 `options.target` 为绝对路径时抛出错误
138
- * @description
139
- * 该函数相当于实现 `cpx .claude/agents <target>` 命令。
140
- * 从根目录的 .claude/agents 复制到目标位置。
141
- *
142
- * **安全限制**:`target` 参数必须是相对路径,禁止使用绝对路径,以防止意外覆盖系统目录。
143
- *
144
- * @example
145
- * // ✅ 自动检测 monorepo 根目录,复制到当前目录的 dist 文件夹
146
- * copyClaudeAgents({ target: 'dist' })
147
- *
148
- * // ✅ 手动指定根目录为向上三级,复制到 build 文件夹
149
- * copyClaudeAgents({
150
- * target: 'build',
151
- * rootDir: '../../../'
152
- * })
153
- *
154
- * // ✅ 使用绝对路径指定根目录(rootDir 允许绝对路径)
155
- * copyClaudeAgents({
156
- * target: 'dist', // target 必须是相对路径
157
- * rootDir: '/absolute/path/to/monorepo' // rootDir 可以是绝对路径
158
- * })
159
- *
160
- * @example
161
- * // ❌ 错误用法:target 使用绝对路径会抛出错误
162
- * copyClaudeAgents({ target: '/var/www/agents' }) // 抛出 Error
163
- * copyClaudeAgents({ target: 'C:\\Windows\\agents' }) // 抛出 Error
164
- */
165
- export function copyClaudeAgents(options: CopyClaudeAgentsOptions): void {
166
- // 验证 target 不能是绝对路径
167
- if (path.isAbsolute(options.target)) {
168
- const errorMessage = [
169
- `target 参数不允许使用绝对路径,这可能导致意外的文件覆盖。`,
170
- `当前传入的路径: "${options.target}"`,
171
- `请使用相对路径,例如: "src/docs/prompts/agents"`,
172
- ].join("\n");
173
-
174
- consola.error(errorMessage);
175
- throw new Error(errorMessage);
176
- }
177
-
178
- // 检查源目录是否存在
179
- if (!hasClaudeAgents({ rootDir: options.rootDir })) {
180
- return;
181
- }
182
-
183
- const root = resolveRootDir(options.rootDir);
184
- const source = path.join(root, ".claude/agents");
185
- const destination = path.resolve(process.cwd(), options.target);
186
-
187
- // 确保目标文件夹的父目录存在
188
- fs.mkdirSync(path.dirname(destination), { recursive: true });
189
-
190
- // 递归复制文件夹
191
- fs.cpSync(source, destination, { recursive: true });
192
-
193
- consola.success(`已成功复制 .claude/agents 到 ${destination}`);
194
- }
@@ -1,58 +0,0 @@
1
- import fs from "node:fs";
2
- import path from "node:path";
3
- import consola from "consola";
4
-
5
- import { isConditionsSome } from "../../conditions";
6
-
7
- /** 大写字母的文件 */
8
- const capitalReadmeMd = "README.md" as const;
9
-
10
- /** 小写字母的文件 */
11
- const lowerCaseReadmeMd = "readme.md" as const;
12
-
13
- /** 检查当前运行的根目录 是否存在文件名大写的 `README.md` 文件 */
14
- export function hasCapitalReadmeMd() {
15
- const res = fs.existsSync(path.resolve(process.cwd(), capitalReadmeMd));
16
- if (!res) {
17
- consola.warn(`当前项目根目录不存在 ${capitalReadmeMd} 文件`);
18
- }
19
- return res;
20
- }
21
-
22
- /** 检查当前运行的根目录 是否存在文件名小写的 `readme.md` 文件 */
23
- export function hasLowerCaseReadmeMd() {
24
- const res = fs.existsSync(path.resolve(process.cwd(), lowerCaseReadmeMd));
25
- if (!res) {
26
- consola.log(`当前项目根目录不存在 ${lowerCaseReadmeMd} 文件`);
27
- }
28
- return res;
29
- }
30
-
31
- /** 检查当前运行的根目录 是否存在任意一个大小写命名的 README.md 文件 */
32
- export function hasReadmeMd() {
33
- const res = isConditionsSome([() => hasCapitalReadmeMd(), () => hasCapitalReadmeMd()]);
34
- return res;
35
- }
36
-
37
- /**
38
- * 将 README.md 文件移动到指定要求的位置内,并重命名为 index.md
39
- * @description
40
- * 该函数相当于实现 `cpx README.md docs` 命令
41
- */
42
- export function copyReadmeMd(/** 目标文件夹 */ target: string) {
43
- if (!hasReadmeMd()) {
44
- return;
45
- }
46
-
47
- let readmeFileName: string = capitalReadmeMd;
48
- if (hasCapitalReadmeMd()) {
49
- readmeFileName = capitalReadmeMd;
50
- } else if (hasLowerCaseReadmeMd()) {
51
- readmeFileName = lowerCaseReadmeMd;
52
- }
53
-
54
- const source = path.resolve(process.cwd(), readmeFileName);
55
- const destination = path.resolve(process.cwd(), target, "index.md");
56
-
57
- fs.copyFileSync(source, destination);
58
- }