-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathextract-commit.mjs
More file actions
66 lines (54 loc) · 2.14 KB
/
extract-commit.mjs
File metadata and controls
66 lines (54 loc) · 2.14 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
import { execSync } from 'child_process'
import fs from 'fs/promises'
import { resolve } from 'path'
import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const commitInfoDir = resolve(__dirname, 'docs') // 文件夹路径
const commitInfoPath = resolve(__dirname, 'docs/src', 'commitInfo.js') // 文件路径
console.log('Directory:', commitInfoDir)
console.log('File Path:', commitInfoPath)
// 检查文件是否存在,如果不存在,则创建文件
async function createFileIfNotExists(filePath) {
try {
await fs.access(filePath) // 检查文件是否存在
} catch (error) {
// 如果文件不存在,创建文件并初始化为空
await fs.writeFile(filePath, '')
console.log('File did not exist, creating it now...')
}
}
// 写入 commit 信息到文件
async function extractCommitInfo() {
try {
// 确保文件存在
await createFileIfNotExists(commitInfoPath)
// 执行 Git 命令获取 commit 消息
const commitMessagesRaw = execSync('git log --pretty=%B').toString()
// 执行 Git 命令获取所有提交的时间
const commitTime = execSync('git log --pretty=%ad --date=format:"%Y-%m-%d %H:%M:%S"')
.toString()
.trim()
.split('\n')
const commitHash = execSync('git log --pretty=format:"%h"').toString().trim().split('\n')
// 使用正则表达式按两个换行符分割 message
const commitMessages = commitMessagesRaw.split(/\n\n/).map((msg) => msg.trim())
// 创建新的 commit 对象数组
const newCommitEntries = commitMessages.map((i, idx) => {
return `{
hash: ${JSON.stringify(commitHash[idx])},
message:${JSON.stringify(i)},
commitTime:${JSON.stringify(commitTime[idx])}
}`
})
// 将 commit 信息格式化为对象
const commitInfo = `export const commitHistory = [
${newCommitEntries}
];`
// 写入 commit 信息到文件
await fs.writeFile(commitInfoPath, commitInfo)
console.log('Commit information has been extracted.')
} catch (error) {
console.error('Error writing commit information:', error.message)
}
}
// extractCommitInfo()