forked from jaredpalmer/after.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease-notes.js
119 lines (100 loc) · 2.93 KB
/
release-notes.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const got = require('got');
async function main() {
const res = await got(
'https://api.github.com/repos/jaredpalmer/after.js/releases',
{
responseType: 'json',
}
);
const allReleases = res.body
.map(({ id, tag_name, created_at, body }) => ({
id,
tag_name,
created_at,
body: body
.replace(/\r\n/g, '\n')
.split('\n')
.map(e => e.trim()),
}))
.sort((a, b) => a.created_at.localeCompare(b.created_at));
const targetVersion = /v(.*?-)/
.exec(allReleases.filter(e => /v.*?-/.exec(e.tag_name)).pop().tag_name)
.pop();
const releases = allReleases.filter(v => v.tag_name.includes(targetVersion));
const lineItems = {
'### Minor Changes': [],
'### Patches': [],
'### Credits': [],
};
Object.keys(lineItems).forEach(header => {
releases.forEach(release => {
const headerIndex = release.body.indexOf(header);
if (!~headerIndex) return;
let headerLastIndex = release.body
.slice(headerIndex + 1)
.findIndex(v => v.startsWith('###'));
if (~headerLastIndex) {
headerLastIndex = headerLastIndex + headerIndex;
} else {
headerLastIndex = release.body.length - 1;
}
if (header === '### Credits') {
release.body.slice(headerIndex, headerLastIndex + 1).forEach(e => {
const re = /@[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}/gi;
let m;
do {
m = re.exec(e);
if (m) {
lineItems[header].push(m.pop());
}
} while (m);
});
} else {
release.body.slice(headerIndex, headerLastIndex + 1).forEach(e => {
if (!e.startsWith('-')) {
return;
}
lineItems[header].push(e);
});
}
});
});
let finalMessage = [];
Object.keys(lineItems).forEach(header => {
let items = lineItems[header];
if (!items.length) {
return;
}
finalMessage.push(header);
finalMessage.push('');
if (header === '### Credits') {
items = [...new Set(items)];
let creditsMessage = `Huge thanks to `;
if (items.length > 1) {
creditsMessage += items.slice(0, items.length - 1).join(`, `);
creditsMessage += `, and `;
}
creditsMessage += items[items.length - 1];
creditsMessage += ` for helping!`;
finalMessage.push(creditsMessage);
} else {
items.forEach(e => finalMessage.push(e));
}
finalMessage.push('');
});
const firstVersion = releases[0].tag_name;
const lastVersion = releases[releases.length - 1].tag_name;
// Add compare link
finalMessage.push(
`https://github.com/jaredpalmer/after.js/compare/${firstVersion}...${lastVersion}`
);
return {
version: targetVersion.slice(0, -1),
firstVersion,
lastVersion,
content: finalMessage.join('\n'),
};
}
Promise.resolve()
.then(main)
.then(notes => console.log(String(notes.content)));