-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathchanglog.js
More file actions
72 lines (60 loc) · 2.11 KB
/
Copy pathchanglog.js
File metadata and controls
72 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable no-undef */
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
/**
* 生成changelog并处理文件内容
*/
function generateChangelog() {
try {
console.log("🚀 开始生成 CHANGELOG.md...");
// 执行 npm run changlog 命令
console.log("📝 执行 gitmoji-changelog 生成changelog...");
execSync("gitmoji-changelog init --author=true --group-similar-commits=true", {
stdio: "inherit",
cwd: process.cwd(),
});
console.log("✅ changelog 生成完成");
// 读取生成的 CHANGELOG.md 文件
const changelogPath = path.join(process.cwd(), "CHANGELOG.md");
if (!fs.existsSync(changelogPath)) {
console.error("❌ CHANGELOG.md 文件不存在");
process.exit(1);
}
console.log("📖 读取 CHANGELOG.md 文件...");
let content = fs.readFileSync(changelogPath, "utf8");
// 使用正则表达式替换 (by (\w) -> (by @$1
// 删除owner
console.log("🔄 处理文件内容,添加 @ 符号...");
// 处理用户名
let usernameMap = {
王一之: "CodFrm",
CodFrm: "CodFrm",
wangyizhi: "CodFrm",
};
for (const [name, username] of Object.entries(usernameMap)) {
const regex = new RegExp(`\\(by ${name}\\)`, "g");
content = content.replaceAll(regex, `(by @${username})`);
}
const updatedContent = content.replace(/\(by (\w)/g, "(by @$1");
// 检查是否有内容被替换
if (content !== updatedContent) {
// 写回文件
fs.writeFileSync(changelogPath, updatedContent, "utf8");
console.log("✅ 文件内容已更新,作者名前已添加 @ 符号");
} else {
console.log("ℹ️ 没有找到需要替换的内容");
}
console.log("🎉 CHANGELOG.md 处理完成!");
} catch (error) {
console.error("❌ 生成changelog时出错:", error.message);
process.exit(1);
}
}
// 如果直接运行此脚本
if (require.main === module) {
generateChangelog();
}
module.exports = { generateChangelog };