/
merge-config.js
372 lines (317 loc) · 9.56 KB
/
merge-config.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import fs from 'fs/promises'
import path from 'path'
import { exec } from 'child_process'
import { URL, fileURLToPath } from 'url'
import hostedGitInfo from 'hosted-git-info'
import parseRepositoryUrl from '@hutson/parse-repository-url'
import { getSemverTags } from 'git-semver-tags'
import normalizePackageData from 'normalize-package-data'
const dirname = fileURLToPath(new URL('.', import.meta.url))
const rhosts = /github|bitbucket|gitlab/i
// sv-SEis used for yyyy-mm-dd format
const dateFormatter = Intl.DateTimeFormat('sv-SE', {
timeZone: 'UTC'
})
function guessNextTag (previousTag, version) {
if (previousTag) {
if (previousTag[0] === 'v' && version[0] !== 'v') {
return 'v' + version
}
if (previousTag[0] !== 'v' && version[0] === 'v') {
return version.replace(/^v/, '')
}
return version
}
if (version[0] !== 'v') {
return 'v' + version
}
return version
}
function omitUndefinedValueProps (obj) {
if (!obj) {
return {}
}
const omittedObj = {}
for (const key in obj) {
if (obj[key] !== undefined) {
omittedObj[key] = obj[key]
}
}
return omittedObj
}
function getRemoteOriginUrl (cwd) {
return new Promise((resolve, reject) => {
exec('git config --get remote.origin.url', { cwd }, (err, stdout) => {
if (err) {
reject(err)
} else {
resolve(stdout.trim())
}
})
})
}
export default async function mergeConfig (options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) {
let pkgPromise
options = omitUndefinedValueProps(options)
context = context || {}
gitRawCommitsOpts = gitRawCommitsOpts || {}
gitRawExecOpts = {
cwd: options?.cwd,
...gitRawExecOpts || {}
}
const rtag = options && options.tagPrefix ? new RegExp(`tag:\\s*[=]?${options.tagPrefix}(.+?)[,)]`, 'gi') : /tag:\s*[v=]?(.+?)[,)]/gi
options = {
append: false,
releaseCount: 1,
skipUnstable: false,
debug: function () {},
transform: function (commit, cb) {
if (typeof commit.gitTags === 'string') {
const match = rtag.exec(commit.gitTags)
rtag.lastIndex = 0
if (match) {
commit.version = match[1]
}
}
if (commit.committerDate) {
commit.committerDate = dateFormatter.format(new Date(commit.committerDate))
}
cb(null, commit)
},
lernaPackage: null,
...options,
pkg: {
transform: function (pkg) {
return pkg
},
...options?.pkg
}
}
options.warn = options.warn || options.debug
if (options.pkg) {
if (options.pkg.path) {
pkgPromise = import('read-pkg').then(async ({ parsePackage }) => {
const json = await fs.readFile(options.pkg.path, 'utf-8')
return parsePackage(json)
})
} else {
pkgPromise = import('read-package-up').then(async ({ readPackageUp }) => {
const { packageJson } = await readPackageUp({ cwd: options.cwd })
return packageJson
})
}
}
const presetConfig = typeof options.config === 'function' ? options.config() : options.config
const [
configObj,
pkgObj,
tagsObj,
gitRemoteOriginUrlObj
] = await Promise.allSettled([
presetConfig,
pkgPromise,
getSemverTags({
lernaTags: !!options.lernaPackage,
package: options.lernaPackage,
tagPrefix: options.tagPrefix,
skipUnstable: options.skipUnstable,
cwd: options.cwd
}),
getRemoteOriginUrl(options.cwd)
])
let config
let pkg
let fromTag
let repo
let hostOpts
let semverTags = []
if (options.config) {
if (configObj.status === 'fulfilled') {
config = configObj.value
} else {
options.warn(configObj.reason.toString())
config = {}
}
} else {
config = {}
}
context = {
...context,
...config.context
}
if (options.pkg) {
if (pkgObj.status === 'fulfilled') {
pkg = pkgObj.value || {}
pkg = options.pkg.transform(pkg)
} else if (options.pkg.path) {
options.warn(pkgObj.reason.toString())
}
}
if ((!pkg || !pkg.repository || !pkg.repository.url) && gitRemoteOriginUrlObj.status === 'fulfilled') {
pkg = pkg || {}
pkg.repository = pkg.repository || {}
pkg.repository.url = gitRemoteOriginUrlObj.value
normalizePackageData(pkg)
}
if (pkg) {
context.version = context.version || pkg.version
try {
const repositoryURL = typeof pkg.repository === 'string' ? pkg.repository : pkg.repository.url
if (repositoryURL) {
// Remove parseRepositoryUrl when https://github.com/npm/hosted-git-info/issues/39 is fixed
repo = hostedGitInfo.fromUrl(repositoryURL) || parseRepositoryUrl(repositoryURL)
}
} catch (err) {
repo = {}
}
if (repo.browse) {
const browse = repo.browse()
if (!context.host) {
if (repo.domain) {
const parsedBrowse = new URL(browse)
if (parsedBrowse.origin.indexOf('//') !== -1) {
context.host = parsedBrowse.protocol + '//' + repo.domain
} else {
context.host = parsedBrowse.protocol + repo.domain
}
} else {
context.host = null
}
}
context.owner = context.owner || repo.user || ''
context.repository = context.repository || repo.project
if (repo.host && repo.project && repo.user) {
context.repoUrl = browse
} else {
context.repoUrl = context.host
}
}
context.packageData = pkg
}
context.version = context.version || ''
if (tagsObj.status === 'fulfilled') {
semverTags = context.gitSemverTags = tagsObj.value
fromTag = semverTags[options.releaseCount - 1]
const lastTag = semverTags[0]
if (lastTag === context.version || lastTag === 'v' + context.version) {
if (options.outputUnreleased) {
context.version = 'Unreleased'
} else {
options.outputUnreleased = false
}
}
}
if (typeof options.outputUnreleased !== 'boolean') {
options.outputUnreleased = true
}
if (context.host && (!context.issue || !context.commit || !parserOpts || !parserOpts.referenceActions)) {
let type
if (context.host) {
const match = context.host.match(rhosts)
if (match) {
type = match[0]
}
} else if (repo && repo.type) {
type = repo.type
}
if (type) {
hostOpts = JSON.parse(await fs.readFile(path.join(dirname, '..', 'hosts', `${type}.json`), 'utf8'))
context = {
issue: hostOpts.issue,
commit: hostOpts.commit,
...context
}
} else {
options.warn('Host: "' + context.host + '" does not exist')
hostOpts = {}
}
} else {
hostOpts = {}
}
if (context.resetChangelog) {
fromTag = null
}
gitRawCommitsOpts = {
format: '%B%n-hash-%n%H%n-gitTags-%n%d%n-committerDate-%n%ci',
from: fromTag,
merges: false,
debug: options.debug,
...config.commits,
...gitRawCommitsOpts
}
if (options.append) {
gitRawCommitsOpts.reverse = gitRawCommitsOpts.reverse || true
}
parserOpts = {
...config.parser,
warn: options.warn,
...parserOpts
}
if (hostOpts.referenceActions && parserOpts) {
parserOpts.referenceActions = hostOpts.referenceActions
}
if (!parserOpts.issuePrefixes?.length && hostOpts.issuePrefixes) {
parserOpts.issuePrefixes = hostOpts.issuePrefixes
}
writerOpts = {
finalizeContext: function (context, writerOpts, filteredCommits, keyCommit, originalCommits) {
const firstCommit = originalCommits[0]
const lastCommit = originalCommits[originalCommits.length - 1]
const firstCommitHash = firstCommit ? firstCommit.hash : null
const lastCommitHash = lastCommit ? lastCommit.hash : null
if ((!context.currentTag || !context.previousTag) && keyCommit) {
const match = /tag:\s*(.+?)[,)]/gi.exec(keyCommit.gitTags)
const currentTag = context.currentTag
context.currentTag = currentTag || match ? match[1] : null
const index = semverTags.indexOf(context.currentTag)
// if `keyCommit.gitTags` is not a semver
if (index === -1) {
context.currentTag = currentTag || null
} else {
const previousTag = context.previousTag = semverTags[index + 1]
if (!previousTag) {
if (options.append) {
context.previousTag = context.previousTag || firstCommitHash
} else {
context.previousTag = context.previousTag || lastCommitHash
}
}
}
} else {
context.previousTag = context.previousTag || semverTags[0]
if (context.version === 'Unreleased') {
if (options.append) {
context.currentTag = context.currentTag || lastCommitHash
} else {
context.currentTag = context.currentTag || firstCommitHash
}
} else if (!context.currentTag) {
if (options.lernaPackage) {
context.currentTag = options.lernaPackage + '@' + context.version
} else if (options.tagPrefix) {
context.currentTag = options.tagPrefix + context.version
} else {
context.currentTag = guessNextTag(semverTags[0], context.version)
}
}
}
if (typeof context.linkCompare !== 'boolean' && context.previousTag && context.currentTag) {
context.linkCompare = true
}
return context
},
debug: options.debug,
...config.writer,
reverse: options.append,
doFlush: options.outputUnreleased,
...writerOpts
}
return {
options,
context,
gitRawCommitsOpts,
parserOpts,
writerOpts,
gitRawExecOpts
}
}