-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathenhance-changelog.js
41 lines (35 loc) · 1.4 KB
/
enhance-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
const { execSync, spawnSync } = require('child_process');
const fs = require('fs');
const tag = execSync('git describe --tags --abbrev=0').toString().trim();
console.log('Checking commits since', tag);
const args = ['-n', '1', `${tag}..HEAD`, '--abbrev=7', '--pretty=%h', '--', 'CHANGELOG.md'];
function findCommit(line) {
const result = spawnSync('git', ['log', '-S', line, ...args], { shell: false });
if (result.status === 0) return result.stdout.toString().trim();
throw new Error(result.stderr.toString());
}
const lines = fs.readFileSync('CHANGELOG.md').toString().split(/\r?\n/g);
let enhanced = 0;
let shouldEnhance = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('## Unreleased')) shouldEnhance = true;
else if (line.startsWith('## ')) break;
if (!line.startsWith('- ')) continue;
const commit = findCommit(line);
if (!commit) return;
console.log(line, '=>', commit);
const brackets = line.match(/ \((.*?)\)$/);
if (brackets) {
if (brackets[1].match(/[\da-fA-F]{7}/)) continue;
if (!brackets[1].includes(' ')) {
lines[i] = line.replace(/\(.*?\)$/, `(${commit}, ${brackets[1]})`);
enhanced++;
continue;
}
}
lines[i] = `${line} (${commit})`;
enhanced++;
}
console.log(`Enhanced ${enhanced} lines`);
fs.writeFileSync('CHANGELOG.md', lines.join('\n'));