|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Generate a changelog between two dates |
| 5 | + * Usage: node scripts/changelog-dates.js --from YYYY-MM-DD --to YYYY-MM-DD |
| 6 | + */ |
| 7 | + |
| 8 | +const { execSync } = require('child_process'); |
| 9 | +const { argv } = require('process'); |
| 10 | + |
| 11 | +// Parse command line arguments |
| 12 | +function parseArgs() { |
| 13 | + const args = {}; |
| 14 | + for (let i = 2; i < argv.length; i++) { |
| 15 | + if (argv[i] === '--from' && argv[i + 1]) { |
| 16 | + args.from = argv[i + 1]; |
| 17 | + i++; |
| 18 | + } else if (argv[i] === '--to' && argv[i + 1]) { |
| 19 | + args.to = argv[i + 1]; |
| 20 | + i++; |
| 21 | + } |
| 22 | + } |
| 23 | + return args; |
| 24 | +} |
| 25 | + |
| 26 | +// Validate date format (YYYY-MM-DD) |
| 27 | +function isValidDate(dateString) { |
| 28 | + const regex = /^\d{4}-\d{2}-\d{2}$/; |
| 29 | + if (!regex.test(dateString)) return false; |
| 30 | + const date = new Date(dateString); |
| 31 | + return date instanceof Date && !isNaN(date); |
| 32 | +} |
| 33 | + |
| 34 | +// Get commits between two dates |
| 35 | +function getCommits(fromDate, toDate) { |
| 36 | + try { |
| 37 | + const since = `${fromDate} 00:00:00`; |
| 38 | + const until = `${toDate} 23:59:59`; |
| 39 | + const command = `git log --since="${since}" --until="${until}" --pretty=format:"%h|%s|%an|%ad" --date=short`; |
| 40 | + const output = execSync(command, { encoding: 'utf-8' }); |
| 41 | + return output.trim().split('\n').filter(line => line.length > 0); |
| 42 | + } catch (error) { |
| 43 | + return []; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Categorize commit by conventional commit type |
| 48 | +function categorizeCommit(message) { |
| 49 | + const types = { |
| 50 | + feat: 'Added', |
| 51 | + fix: 'Fixed', |
| 52 | + docs: 'Documentation', |
| 53 | + style: 'Style', |
| 54 | + refactor: 'Changed', |
| 55 | + perf: 'Performance', |
| 56 | + test: 'Tests', |
| 57 | + chore: 'Chore', |
| 58 | + build: 'Build', |
| 59 | + ci: 'CI' |
| 60 | + }; |
| 61 | + |
| 62 | + for (const [type, category] of Object.entries(types)) { |
| 63 | + if (message.toLowerCase().startsWith(`${type}:`)) { |
| 64 | + return category; |
| 65 | + } |
| 66 | + if (message.toLowerCase().startsWith(`${type}(`)) { |
| 67 | + return category; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Check for breaking changes |
| 72 | + if (message.includes('BREAKING CHANGE') || message.includes('!:')) { |
| 73 | + return 'Breaking Changes'; |
| 74 | + } |
| 75 | + |
| 76 | + return 'Other'; |
| 77 | +} |
| 78 | + |
| 79 | +// Format commits into changelog sections |
| 80 | +function formatChangelog(commits, fromDate, toDate) { |
| 81 | + const categorized = {}; |
| 82 | + |
| 83 | + commits.forEach(line => { |
| 84 | + const [hash, message, author, date] = line.split('|'); |
| 85 | + const category = categorizeCommit(message); |
| 86 | + |
| 87 | + if (!categorized[category]) { |
| 88 | + categorized[category] = []; |
| 89 | + } |
| 90 | + |
| 91 | + // Clean up message (remove type prefix) |
| 92 | + let cleanMessage = message; |
| 93 | + const match = message.match(/^[^:)]+[):]\s*(.+)/); |
| 94 | + if (match) { |
| 95 | + cleanMessage = match[1].trim(); |
| 96 | + } |
| 97 | + |
| 98 | + categorized[category].push({ |
| 99 | + hash: hash.substring(0, 7), |
| 100 | + message: cleanMessage, |
| 101 | + author, |
| 102 | + date |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + // Build changelog output |
| 107 | + let output = `# Changelog: ${fromDate} to ${toDate}\n\n`; |
| 108 | + output += `Generated changelog for the period between ${fromDate} and ${toDate}.\n\n`; |
| 109 | + |
| 110 | + const order = [ |
| 111 | + 'Breaking Changes', |
| 112 | + 'Added', |
| 113 | + 'Fixed', |
| 114 | + 'Changed', |
| 115 | + 'Performance', |
| 116 | + 'Documentation', |
| 117 | + 'Build', |
| 118 | + 'CI', |
| 119 | + 'Tests', |
| 120 | + 'Style', |
| 121 | + 'Chore', |
| 122 | + 'Other' |
| 123 | + ]; |
| 124 | + |
| 125 | + order.forEach(category => { |
| 126 | + if (categorized[category] && categorized[category].length > 0) { |
| 127 | + output += `## ${category}\n\n`; |
| 128 | + categorized[category].forEach(commit => { |
| 129 | + output += `- ${commit.message} (${commit.hash}) - ${commit.author}\n`; |
| 130 | + }); |
| 131 | + output += '\n'; |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + // Summary |
| 136 | + const total = commits.length; |
| 137 | + output += `---\n\n**Total commits:** ${total}\n`; |
| 138 | + output += `**Period:** ${fromDate} to ${toDate}\n`; |
| 139 | + |
| 140 | + return output; |
| 141 | +} |
| 142 | + |
| 143 | +// Main execution |
| 144 | +function main() { |
| 145 | + const args = parseArgs(); |
| 146 | + |
| 147 | + if (!args.from || !args.to) { |
| 148 | + console.error('Usage: node scripts/changelog-dates.js --from YYYY-MM-DD --to YYYY-MM-DD'); |
| 149 | + console.error('Example: node scripts/changelog-dates.js --from 2025-01-01 --to 2025-01-31'); |
| 150 | + process.exit(1); |
| 151 | + } |
| 152 | + |
| 153 | + if (!isValidDate(args.from)) { |
| 154 | + console.error(`Invalid date format for --from: ${args.from}`); |
| 155 | + console.error('Expected format: YYYY-MM-DD (e.g., 2025-01-01)'); |
| 156 | + process.exit(1); |
| 157 | + } |
| 158 | + |
| 159 | + if (!isValidDate(args.to)) { |
| 160 | + console.error(`Invalid date format for --to: ${args.to}`); |
| 161 | + console.error('Expected format: YYYY-MM-DD (e.g., 2025-01-31)'); |
| 162 | + process.exit(1); |
| 163 | + } |
| 164 | + |
| 165 | + if (new Date(args.from) > new Date(args.to)) { |
| 166 | + console.error('Error: --from date must be before --to date'); |
| 167 | + process.exit(1); |
| 168 | + } |
| 169 | + |
| 170 | + const commits = getCommits(args.from, args.to); |
| 171 | + |
| 172 | + if (commits.length === 0) { |
| 173 | + console.log(`No commits found between ${args.from} and ${args.to}`); |
| 174 | + process.exit(0); |
| 175 | + } |
| 176 | + |
| 177 | + const changelog = formatChangelog(commits, args.from, args.to); |
| 178 | + console.log(changelog); |
| 179 | +} |
| 180 | + |
| 181 | +main(); |
| 182 | + |
0 commit comments