-
-
Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathchangelog-parser.js
67 lines (57 loc) · 1.8 KB
/
changelog-parser.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
const parseChangelog = require('changelog-parser')
const path = require('path')
const scopes = ['mobile', 'web', 'desktop']
async function parsePackages(packageNames) {
let result = ''
let index = 0
for (const package of packageNames) {
const parsed = await parseChangelog(path.join(__dirname, `../packages/${package}/CHANGELOG.md`))
const latest = parsed.versions[0]
if (packageNames.length > 1) {
result += `## ${capitalizeFirstLetter(package)} Changes\n`
}
result += `${latest.body}\n`
if (index !== packageNames.length - 1) {
result += '\n'
}
index++
}
return cleanOutputString(result)
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
function formatLine(line) {
for (const scope of scopes) {
const formattedScope = `**${scope}:**`
if (line.includes(formattedScope)) {
const parts = line.split(formattedScope)
const hasBulletAndScope = parts.length === 2
if (hasBulletAndScope) {
return `${parts[0]}**${capitalizeFirstLetter(scope)}**: ${capitalizeFirstLetter(parts[1].trim())}`
} else {
return line.replace(formattedScope, `**${capitalizeFirstLetter(scope)}:**`)
}
}
}
const bulletLinePrefix = '* '
const bulletParts = line.split(bulletLinePrefix)
const hasBulletOnly = bulletParts.length === 2
if (hasBulletOnly) {
return `${bulletLinePrefix} ${capitalizeFirstLetter(bulletParts[1])}`
}
return line
}
function cleanOutputString(string) {
const lines = string.split('\n')
const outLines = []
for (const line of lines) {
const outLine = formatLine(line)
outLines.push(outLine)
}
return outLines.join('\n')
}
const packages = process.argv.slice(2);
parsePackages(packages).then((result) => {
process.stdout.write(result)
})