forked from plone/volto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelogupdater.js
89 lines (75 loc) · 2.74 KB
/
changelogupdater.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* eslint no-console: 0 */
const fs = require('fs');
const semver = require('semver');
const args = process.argv;
if (process.argv.length < 3) {
console.log(
'Not enough arguments. You should supply one of: bump || excerpt || back',
);
return;
}
const command = process.argv[2];
const version = process.argv[3];
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
try {
if (command === 'excerpt') {
const data = fs.readFileSync('CHANGELOG.md', 'utf8');
const allReleases = data.match(/##\s(.+)\s\((.+)\)/g);
// const re = new RegExp(escapeRegExp(allReleases[1]), 'g');
const index = data.indexOf(allReleases[1]);
let versionExcerpt = data.slice(0, index);
versionExcerpt = versionExcerpt.replace('# Change Log\n\n', '');
versionExcerpt = versionExcerpt.slice(
versionExcerpt.match('\n\n')['index'] + 2,
);
process.stdout.write(versionExcerpt);
return;
}
if (command === 'bump') {
const data = fs.readFileSync('CHANGELOG.md', 'utf8');
const [original, origVersion, orig] = data.match(/##\s(.+)\s\((.+)\)/);
if (orig !== 'unreleased') {
return console.log('Error, the CHANGELOG file is malformed.');
}
const currentDate = new Date();
const date = `${currentDate.getFullYear()}-${`0${
currentDate.getMonth() + 1
}`.slice(-2)}-${`0${currentDate.getDate()}`.slice(-2)}`;
if (version !== origVersion) {
console.log(`Updating to the given version ${version}`);
}
const newLine = original
.replace('unreleased', date)
.replace(origVersion, version);
const newChangelog = data.replace(original, newLine);
// Save data to disk if command is bump
fs.writeFile('CHANGELOG.md', newChangelog, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('Updated version on CHANGELOG.md');
});
}
if (command === 'back') {
const data = fs.readFileSync('CHANGELOG.md', 'utf8');
const nextversion = semver.inc(process.argv[3], 'patch');
const backToDevelTemplate = `\n\n## ${nextversion} (unreleased)\n\n### Breaking\n\n### Feature\n\n### Bugfix\n\n### Internal\n\n### Documentation`;
const insertIndex = data.indexOf('\n\n');
const back = `${data.slice(
0,
insertIndex,
)}${backToDevelTemplate}${data.slice(insertIndex)}`;
console.log(back);
fs.writeFile('CHANGELOG.md', back, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('Back to development on CHANGELOG.md');
});
}
return;
} catch (e) {
console.log('Error:', e.stack);
}