@ruan-cat/utils 4.12.1 → 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/dist/node-cjs/index.d.cts +1 -1
- package/dist/node-esm/index.d.ts +1 -100
- package/dist/node-esm/index.js +7 -148
- package/dist/node-esm/index.js.map +1 -1
- package/package.json +2 -2
- package/src/.vitepress/config.mts +4 -2
- package/src/node-esm/index.ts +1 -5
- package/src/node-esm/{scripts/yaml-to-md.ts → yaml-to-md.ts} +0 -13
- package/src/node-esm/scripts/add-changelog-to-doc.ts +0 -29
- package/src/node-esm/scripts/copy-changelog.ts +0 -29
- package/src/node-esm/scripts/copy-claude-agents.ts +0 -154
- package/src/node-esm/scripts/copy-readme.ts +0 -58
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/utils",
|
|
3
|
-
"version": "4.
|
|
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": "./
|
|
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",
|
package/src/node-esm/index.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
export * from "./ruan-cat-pkg-info";
|
|
2
2
|
|
|
3
3
|
export * from "./scripts/clean";
|
|
4
|
-
export * from "./
|
|
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,154 +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
|
-
* @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
|
-
}
|
|
@@ -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
|
-
}
|