@ruan-cat/utils 4.0.0 → 4.1.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-esm/index.d.ts +30 -1
- package/dist/node-esm/index.js +2694 -1
- package/dist/node-esm/index.js.map +1 -1
- package/package.json +9 -9
- package/src/node-esm/index.ts +3 -0
- package/src/node-esm/scripts/add-changelog-to-doc.ts +29 -0
- package/src/node-esm/scripts/copy-changelog.ts +26 -0
- package/src/node-esm/scripts/yaml-to-md.ts +69 -0
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
consola.log("当前项目根目录为:", process.cwd());
|
|
8
|
+
consola.warn("当前项目根目录不存在 CHANGELOG.md 文件");
|
|
9
|
+
return fs.existsSync(path.resolve(process.cwd(), "CHANGELOG.md"));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 将 CHANGELOG.md 文件移动到指定要求的位置内
|
|
14
|
+
* @description
|
|
15
|
+
* 该函数相当于实现 `cpx CHANGELOG.md docs` 命令
|
|
16
|
+
*/
|
|
17
|
+
export function copyChangelogMd(/** 目标文件夹 */ target: string) {
|
|
18
|
+
if (!hasChangelogMd()) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const source = path.resolve(process.cwd(), "CHANGELOG.md");
|
|
23
|
+
const destination = path.resolve(process.cwd(), target, "CHANGELOG.md");
|
|
24
|
+
|
|
25
|
+
fs.copyFileSync(source, destination);
|
|
26
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
import { consola } from "consola";
|
|
4
|
+
import { isUndefined } from "lodash-es";
|
|
5
|
+
import yaml from "js-yaml";
|
|
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
|
+
export interface WriteYaml2mdParams<T = Record<string, any>> {
|
|
21
|
+
/** 目标md文件地址 */
|
|
22
|
+
mdPath: string;
|
|
23
|
+
|
|
24
|
+
/** 被插入到md头部的数据 */
|
|
25
|
+
data: T;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 将YAML数据写入到MD文件内
|
|
30
|
+
*/
|
|
31
|
+
export function writeYaml2md<T>(params: WriteYaml2mdParams<T>) {
|
|
32
|
+
consola.info(` 当前运行的地址为: ${process.cwd()} `);
|
|
33
|
+
const { mdPath, data } = params;
|
|
34
|
+
|
|
35
|
+
if (isUndefined(mdPath)) {
|
|
36
|
+
consola.error(" 请提供md文件的地址 ");
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check if file exists
|
|
41
|
+
try {
|
|
42
|
+
readFileSync(mdPath, "utf-8");
|
|
43
|
+
} catch (error) {
|
|
44
|
+
consola.error(` 文件 ${mdPath} 不存在 `);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Read the existing MD file
|
|
49
|
+
const mdContent = readFileSync(mdPath, "utf-8");
|
|
50
|
+
|
|
51
|
+
// Convert data to YAML
|
|
52
|
+
const yamlContent = yaml.dump(data);
|
|
53
|
+
|
|
54
|
+
// Combine YAML with MD content
|
|
55
|
+
const newContent = `---\n${yamlContent}---\n\n${mdContent}`;
|
|
56
|
+
|
|
57
|
+
// 警告 暂不考虑使用本函数内的prettier功能 避免打包体积太大
|
|
58
|
+
// Format with prettier using project config
|
|
59
|
+
// const formattedContent = await prettier.format(newContent, {
|
|
60
|
+
// parser: "markdown",
|
|
61
|
+
// ...prettierConfig,
|
|
62
|
+
// });
|
|
63
|
+
|
|
64
|
+
// Write back to file
|
|
65
|
+
// writeFileSync(mdPath, formattedContent, "utf-8");
|
|
66
|
+
writeFileSync(mdPath, newContent, "utf-8");
|
|
67
|
+
|
|
68
|
+
consola.success(` 已将YAML数据写入到 ${mdPath} `);
|
|
69
|
+
}
|