-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (129 loc) · 3.28 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const { exec } = require('child_process')
const R = require('ramda')
const TAG_SPLIT = '\n## '
const debug = x => {
console.log(x)
debugger
return x
}
const prepend = pre => str => `${pre}${str}`
const removeBlanks = R.filter(R.identity)
const isUnreleased = R.startsWith('Unreleased')
const isReleased = R.compose(R.not, isUnreleased)
const isPrerelease = tag => isUnreleased(tag) || tag.match(/v\d*\.\d*\.\d*-/)
const tagFrom = tag => (tag ? `--tag-from ${tag.version}` : '')
const tagTo = tag => (tag ? `--tag-to ${tag.version}` : '')
const parseTagVersion = R.pipe(R.split(' '), R.head, R.trim)
const parseStableTagVersion = R.pipe(
parseTagVersion,
R.split('-'),
R.head,
R.trim
)
const buildTimestampHash = R.reduce((acc, tag) => {
acc[tag.version] = tag.date
return acc
}, {})
const getPrevStableTag = latest =>
R.compose(R.head, R.filter(tag => !tag.version.includes(latest)))
const removePrereleases = tags => {
const versions = {}
return tags.reduce((acc, tag) => {
const version = parseStableTagVersion(tag)
if (!versions[version]) {
versions[version] = true
acc.push(tag)
}
return acc
}, [])
}
const sortTags = timestampHash => mdTags =>
mdTags.sort((a, b) => {
const versionA = parseTagVersion(a)
const versionB = parseTagVersion(b)
return timestampHash[versionB] - timestampHash[versionA]
})
const squashVersions = tags =>
R.pipe(
R.split(TAG_SPLIT),
removeBlanks,
R.filter(isReleased),
removePrereleases,
sortTags(buildTimestampHash(tags)),
R.join(TAG_SPLIT),
prepend(TAG_SPLIT)
)
const squashUnreleased = R.pipe(
R.split(TAG_SPLIT),
removeBlanks,
R.filter(isPrerelease),
R.take(2),
R.join(TAG_SPLIT),
prepend(TAG_SPLIT)
)
const toTag = meta => {
const [version, date] = meta.split('|')
return { version, date: parseInt(date, 10) }
}
const getTags = () =>
new Promise((resolve, reject) =>
exec(
"git for-each-ref --sort=-taggerdate --format '%(tag)|%(taggerdate:raw)' refs/tags",
(err, stdout, stderr) => (err ? reject(err) : resolve(stdout))
)
).then(parseTags)
const parseTags = R.pipe(
R.split('\n'),
R.map(R.trim),
removeBlanks,
R.map(toTag)
)
const lernaChangelog = (from, to) =>
new Promise((resolve, reject) =>
exec(
`npx lerna-changelog ${tagFrom(from)} ${tagTo(to)}`,
(err, stdout, stderr) => (err ? reject(err) : resolve(stdout))
)
)
const fullChangelog = squash =>
getTags().then(tags =>
lernaChangelog(R.last(tags)).then(squash ? squashVersions(tags) : tags)
)
const recentChangelog = () =>
getTags()
.then(tags =>
lernaChangelog(
getPrevStableTag(R.head(tags).version)(tags),
R.head(tags)
).then(squashVersions(tags))
)
.catch(console.error)
const unreleasedChangelog = () =>
getTags()
.then(tags => lernaChangelog(R.head(tags)).then(squashUnreleased))
.catch(console.error)
module.exports = {
buildTimestampHash,
debug,
fullChangelog,
getPrevStableTag,
getTags,
isPrerelease,
isReleased,
isUnreleased,
lernaChangelog,
parseTags,
parseTagVersion,
parseStableTagVersion,
prepend,
recentChangelog,
removeBlanks,
removePrereleases,
sortTags,
squashUnreleased,
squashVersions,
tagFrom,
tagTo,
toTag,
unreleasedChangelog,
}