-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (200 loc) · 6.04 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict'
const { Transform } = require('stream')
const { join } = require('path')
const { readFileSync } = require('fs')
const { valid: semverValid } = require('semver')
const {
functionify,
processCommit,
generate
} = require('./lib/util')
// sv-SE is used for yyyy-mm-dd format
const dateFormatter = Intl.DateTimeFormat('sv-SE', {
timeZone: 'UTC'
})
function conventionalChangelogWriterInit (context, options) {
context = {
commit: 'commits',
issue: 'issues',
date: dateFormatter.format(new Date()),
...context
}
if (typeof context.linkReferences !== 'boolean' && (context.repository || context.repoUrl) && context.commit && context.issue) {
context.linkReferences = true
}
options = {
groupBy: 'type',
commitsSort: 'header',
noteGroupsSort: 'title',
notesSort: 'text',
generateOn: commit => semverValid(commit.version),
finalizeContext: context => context,
debug: () => {},
reverse: false,
includeDetails: false,
ignoreReverted: true,
doFlush: true,
mainTemplate: readFileSync(join(__dirname, 'templates/template.hbs'), 'utf-8'),
headerPartial: readFileSync(join(__dirname, 'templates/header.hbs'), 'utf-8'),
commitPartial: readFileSync(join(__dirname, 'templates/commit.hbs'), 'utf-8'),
footerPartial: readFileSync(join(__dirname, 'templates/footer.hbs'), 'utf-8'),
...options
}
if (!options.transform || typeof options.transform === 'object') {
options.transform = {
hash: function (hash) {
if (typeof hash === 'string') {
return hash.substring(0, 7)
}
},
header: function (header) {
return header.substring(0, 100)
},
committerDate: function (date) {
if (!date) {
return
}
return dateFormatter.format(new Date(date))
},
...options.transform
}
}
let generateOn = options.generateOn
if (typeof generateOn === 'string') {
generateOn = function (commit) {
return typeof commit[options.generateOn] !== 'undefined'
}
} else if (typeof generateOn !== 'function') {
generateOn = function () {
return false
}
}
options.commitGroupsSort = functionify(options.commitGroupsSort)
options.commitsSort = functionify(options.commitsSort)
options.noteGroupsSort = functionify(options.noteGroupsSort)
options.notesSort = functionify(options.notesSort)
return { context, options, generateOn }
}
function conventionalChangelogWriterParseStream (context, options) {
let generateOn
({ context, options, generateOn } = conventionalChangelogWriterInit(context, options))
let commits = []
let neverGenerated = true
let savedKeyCommit
let firstRelease = true
return new Transform({
objectMode: true,
highWaterMark: 16,
async transform (chunk, _enc, cb) {
try {
let result
const commit = await processCommit(chunk, options.transform, context)
const keyCommit = commit || chunk
// previous blocks of logs
if (options.reverse) {
if (commit) {
if (Array.isArray(commit)) {
commits.push(...commit)
} else {
commits.push(commit)
}
}
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = await generate(options, commits, context, keyCommit)
if (options.includeDetails) {
this.push({
log: result,
keyCommit
})
} else {
this.push(result)
}
commits = []
}
} else {
if (generateOn(keyCommit, commits, context, options)) {
neverGenerated = false
result = await generate(options, commits, context, savedKeyCommit)
if (!firstRelease || options.doFlush) {
if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
})
} else {
this.push(result)
}
}
firstRelease = false
commits = []
savedKeyCommit = keyCommit
}
if (commit) {
if (Array.isArray(commit)) {
commits.push(...commit)
} else {
commits.push(commit)
}
}
}
cb()
} catch (err) {
cb(err)
}
},
async flush (cb) {
if (!options.doFlush && (options.reverse || neverGenerated)) {
cb(null)
return
}
try {
const result = await generate(options, commits, context, savedKeyCommit)
if (options.includeDetails) {
this.push({
log: result,
keyCommit: savedKeyCommit
})
} else {
this.push(result)
}
cb()
} catch (err) {
cb(err)
}
}
})
}
/*
* Given an array of commits, returns a string representing a CHANGELOG entry.
*/
conventionalChangelogWriterParseStream.parseArray = async (rawCommits, context, options) => {
let generateOn
rawCommits = [...rawCommits];
({ context, options, generateOn } = conventionalChangelogWriterInit(context, options))
let commits = []
let savedKeyCommit
if (options.reverse) {
rawCommits.reverse()
}
const entries = []
for (const rawCommit of rawCommits) {
const commit = await processCommit(rawCommit, options.transform, context)
const keyCommit = commit || rawCommit
if (generateOn(keyCommit, commits, context, options)) {
entries.push(await generate(options, commits, context, savedKeyCommit))
savedKeyCommit = keyCommit
commits = []
}
if (commit) {
commits.push(commit)
}
}
if (options.reverse) {
entries.reverse()
return await generate(options, commits, context, savedKeyCommit) + entries.join('')
} else {
return entries.join('') + await generate(options, commits, context, savedKeyCommit)
}
}
module.exports = conventionalChangelogWriterParseStream