-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsemver-versioning-strategy.js
71 lines (63 loc) · 2.05 KB
/
semver-versioning-strategy.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
const semver = require('semver')
const { Version } = require('release-please/build/src/version.js')
const inc = (version, release, _preid) => {
const parsed = new semver.SemVer(version)
const implicitPreid = parsed.prerelease.length > 1 ? parsed.prerelease[0]?.toString() : undefined
const preid = _preid || implicitPreid
const next = new semver.SemVer(version).inc(release, preid)
if (!parsed.prerelease.length) {
return next.format()
}
const isFreshMajor = parsed.minor === 0 && parsed.patch === 0
const isFreshMinor = parsed.patch === 0
const shouldPrerelease =
(release === 'premajor' && isFreshMajor) || (release === 'preminor' && isFreshMinor) || release === 'prepatch'
if (shouldPrerelease) {
return semver.inc(version, 'prerelease', preid)
}
return next.format()
}
const parseCommits = commits => {
let release = 'patch'
for (const commit of commits) {
if (commit.breaking) {
release = 'major'
break
} else if (['feat', 'feature'].includes(commit.type)) {
release = 'minor'
}
}
return release
}
class SemverVersioningStrategyNested {
constructor(options, version, commits) {
this.options = options
this.commits = commits
this.version = version
}
bump() {
return new SemverVersioningStrategy(this.options).bump(this.version, this.commits)
}
}
class SemverVersioningStrategy {
constructor(options) {
this.options = options
}
determineReleaseType(version, commits) {
return new SemverVersioningStrategyNested(this.options, version, commits)
}
bump(currentVersion, commits) {
const prerelease = this.options.prerelease
const tag = this.options.prereleaseType
const releaseType = parseCommits(commits)
const addPreIfNeeded = prerelease ? `pre${releaseType}` : releaseType
const version = inc(currentVersion.toString(), addPreIfNeeded, tag)
/* c8 ignore start */
if (!version) {
throw new Error('Could not bump version')
}
/* c8 ignore stop */
return Version.parse(version)
}
}
module.exports = { SemverVersioningStrategy }