generated from MFM-347/RepoLate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchangelog.js
67 lines (57 loc) · 2.08 KB
/
changelog.js
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
import fs from 'fs'
import { execSync } from 'child_process'
function getGitCommits(sinceTag) {
try {
const log = execSync(`git log ${sinceTag}..HEAD --pretty=format:"%h %s"`, {
stdio: 'pipe',
}).toString()
return log
.split('\n')
.filter(Boolean)
.map((line) => {
const [hash, ...messageParts] = line.split(' ')
return { hash, message: messageParts.join(' ') }
})
} catch (error) {
console.error(`Error fetching git commits: ${error.message}`)
return []
}
}
function categorizeCommits(commits) {
const categories = { Features: [], Fixes: [], Chores: [], Updates: [], Others: [] }
commits.forEach(({ hash, message }) => {
const lowerMessage = message.toLowerCase() // Normalize for case variations
if (lowerMessage.startsWith('feat:')) {
categories.Features.push(`- ${message} (${hash})`)
} else if (lowerMessage.startsWith('fix:')) {
categories.Fixes.push(`- ${message} (${hash})`)
} else if (lowerMessage.startsWith('chore:') || lowerMessage.startsWith('chore(')) {
categories.Chores.push(`- ${message} (${hash})`)
} else if (lowerMessage.startsWith('update:') || lowerMessage.startsWith('update ')) {
categories.Updates.push(`- ${message} (${hash})`)
} else {
categories.Others.push(`- ${message} (${hash})`)
}
})
return categories
}
function generateMarkdown(categories) {
let markdown = '## Changes\n\n'
let hasChanges = false
for (const [category, messages] of Object.entries(categories)) {
if (messages.length > 0) {
hasChanges = true
markdown += `### ${category}\n${messages.join('\n')}\n\n`
}
}
return hasChanges ? markdown.trim() : '## Changes\n\nNo changes found.'
}
function writeChangelog(content) {
fs.writeFileSync('CHANGELOG.tmp.md', content)
console.log('CHANGELOG generated successfully.')
}
const sinceTag = process.argv[2] || 'v1.0.0'
const commits = getGitCommits(sinceTag)
const categorizedCommits = categorizeCommits(commits)
const changelogContent = generateMarkdown(categorizedCommits)
writeChangelog(changelogContent)