-
Notifications
You must be signed in to change notification settings - Fork 5
/
prepChangelog.js
60 lines (59 loc) · 2.01 KB
/
prepChangelog.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
const fs = require('fs');
const version = require('./package.json').version;
const devmoji = require('./devmoji.config');
const ACCEPTABLE_ENTRIES = devmoji.devmoji.map(i => i.code);
const CHANGELOG_DIR = './changelog';
function main() {
let files = fs.readdirSync(CHANGELOG_DIR);
files = files.filter(f => f.charAt(0) !== '.');
if (files.length === 0) {
console.log('No changelog entry found!');
return;
}
const currentChangeLog = fs.readFileSync('CHANGELOG.md', 'utf8');
const container = ACCEPTABLE_ENTRIES.reduce((acc, curr) => {
acc[curr] = [];
return acc;
}, {});
let newLog = `### Release v${version}`;
const isNewVersionAdded = !currentChangeLog.includes(newLog);
files.forEach(item => {
const type = item.split('-')[0];
if (ACCEPTABLE_ENTRIES.includes(type)) {
container[type].push(item);
} else {
throw new Error(
'Invalid changelog type, valid types:' + ACCEPTABLE_ENTRIES.join(',')
);
}
});
let updatedStr = '';
Object.keys(container).forEach(item => {
if (container[item].length > 0) {
let sectionHeader = `### ${item}`;
let body = ``;
container[item].forEach((file, idx) => {
const fileContent = fs.readFileSync(`./changelog/${file}`, 'utf8');
const prNumber = file.split('-')[1].replace('.md', '');
const parsedFile = `* ${fileContent.replace(
'\n',
''
)} [#${prNumber}](https://github.com/MyEtherWallet/MyEtherWallet/pull/${prNumber})`;
body = `${body} \n ${parsedFile}`;
if (idx === container[item].length - 1) {
updatedStr = `${updatedStr}\n ${sectionHeader} \n ${body}`;
}
});
}
});
let remadeChangelog = `${updatedStr} \n ${currentChangeLog}`;
if (isNewVersionAdded) {
remadeChangelog = `${newLog} \n ${remadeChangelog}`;
}
fs.writeFileSync(`./CHANGELOG.md`, remadeChangelog);
// // deletes all non release entries for release
files.forEach(item => {
fs.unlinkSync(`${CHANGELOG_DIR}/${item}`);
});
}
main();