1+ #!/usr/bin/env node
2+
3+ const { execSync } = require ( 'child_process' ) ;
4+ const fs = require ( 'fs' ) ;
5+ const path = require ( 'path' ) ;
6+
7+ /**
8+ * 生成changelog并处理文件内容
9+ */
10+ function generateChangelog ( ) {
11+ try {
12+ console . log ( '🚀 开始生成 CHANGELOG.md...' ) ;
13+
14+ // 执行 npm run changlog 命令
15+ console . log ( '📝 执行 gitmoji-changelog 生成changelog...' ) ;
16+ execSync ( 'gitmoji-changelog --author --group-similar-commits' , {
17+ stdio : 'inherit' ,
18+ cwd : process . cwd ( )
19+ } ) ;
20+
21+ console . log ( '✅ changelog 生成完成' ) ;
22+
23+ // 读取生成的 CHANGELOG.md 文件
24+ const changelogPath = path . join ( process . cwd ( ) , 'CHANGELOG.md' ) ;
25+
26+ if ( ! fs . existsSync ( changelogPath ) ) {
27+ console . error ( '❌ CHANGELOG.md 文件不存在' ) ;
28+ process . exit ( 1 ) ;
29+ }
30+
31+ console . log ( '📖 读取 CHANGELOG.md 文件...' ) ;
32+ let content = fs . readFileSync ( changelogPath , 'utf8' ) ;
33+
34+ // 使用正则表达式替换 (by (\w) -> (by @$1
35+ console . log ( '🔄 处理文件内容,添加 @ 符号...' ) ;
36+ const updatedContent = content . replace ( / \( b y ( \w ) / g, '(by @$1' ) ;
37+
38+ // 检查是否有内容被替换
39+ if ( content !== updatedContent ) {
40+ // 写回文件
41+ fs . writeFileSync ( changelogPath , updatedContent , 'utf8' ) ;
42+ console . log ( '✅ 文件内容已更新,作者名前已添加 @ 符号' ) ;
43+ } else {
44+ console . log ( 'ℹ️ 没有找到需要替换的内容' ) ;
45+ }
46+
47+ console . log ( '🎉 CHANGELOG.md 处理完成!' ) ;
48+
49+ } catch ( error ) {
50+ console . error ( '❌ 生成changelog时出错:' , error . message ) ;
51+ process . exit ( 1 ) ;
52+ }
53+ }
54+
55+ // 如果直接运行此脚本
56+ if ( require . main === module ) {
57+ generateChangelog ( ) ;
58+ }
59+
60+ module . exports = { generateChangelog } ;
0 commit comments