-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.release-it.ts
183 lines (173 loc) Β· 5.84 KB
/
.release-it.ts
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
import { Config } from 'release-it'
import { BumperRecommendation, Preset } from 'conventional-recommended-bump'
export const createReleaseItConfig = ({
gitRawCommitsOpts,
}: ExtraConfig = {}): Config => ({
git: {
requireBranch: 'main',
tag: true, // default, but for explicitness
commit: false,
push: true, // default, but for explicitness
},
// @ts-expect-error TODO: Invalid type definition, PR for this
npm: false,
github: {
release: true,
releaseName: 'v${version}',
assets: ['dist/*.zip'],
comments: {
submit: true,
},
},
hooks: {
'before:release': 'pnpm run build:pack',
},
plugins: {
'@release-it/conventional-changelog': {
preset: {
name: 'conventionalcommits',
//π Type list:
// https://github.com/conventional-changelog/conventional-changelog/blob/conventional-changelog-conventionalcommits-v8.0.0/packages/conventional-changelog-conventionalcommits/src/constants.js
//π Emojis:
// https://github.com/orhun/git-cliff/blob/v2.6.1/config/cliff.toml
// https://gitmoji.dev/
types: [
{
type: 'feat',
section: 'π Features',
},
{
type: 'fix',
section: 'π Bug Fixes',
},
{
type: 'perf',
section: 'β‘οΈ Performance Improvements',
},
{
type: 'revert',
section: 'β©οΈ Reverts',
},
{
type: 'docs',
section: 'π Documentation',
},
{
type: 'style',
section: 'π¨ Style',
},
{
type: 'chore',
section: 'πΌ Miscellaneous Chores',
},
{
type: 'refactor',
section: 'β»οΈ Code Refactoring',
},
{
type: 'test',
section: 'π§ͺ Tests',
},
{
type: 'build',
section: 'βοΈ Build System',
},
{
type: 'ci',
section: 'π· Continuous (Integration|Deployment)',
},
],
},
whatBump,
gitRawCommitsOpts,
},
},
})
/**
* β οΈ Conventional Changelog version bumpers always returns at least a patch release
*
* Therefore, `release-it/conventional-changelog` behaves the same, as it's just a wrapper over it
* Here, the recommended version bumper is tuned so that not every commit triggers a version bump.
*
* See:
*
* - GitHub issue about it: https://github.com/release-it/conventional-changelog/issues/22
* - Original implementation: https://github.com/conventional-changelog/conventional-changelog/blob/conventional-recommended-bump-v10.0.0/packages/conventional-changelog-conventionalcommits/src/whatBump.js
*
* @param commits
*/
const whatBump: Preset['whatBump'] = (commits) => {
const commitsByLevel = commits.reduce(
(results, commit) => {
const addToResults = (key: number) => {
results[key].push(commit)
return results
}
// As notes are only parsed for breaking changes
// https://github.com/conventional-changelog/conventional-changelog/blob/conventional-recommended-bump-v10.0.0/packages/conventional-changelog-conventionalcommits/src/parser.js
const hasBreakingChanges = addBangNotes(commit).length > 0
if (hasBreakingChanges) {
return addToResults(RELEASE_LEVEL_MAJOR)
}
if (commit.type === 'feat' || commit.type === 'feature') {
return addToResults(RELEASE_LEVEL_MINOR)
}
const isReleaseCommit = () =>
commit.type === 'chore' &&
commit.scope === 'release' &&
commit.subject?.includes('manual')
if (
commit.type === 'fix' ||
commit.type === 'perf' ||
isReleaseCommit()
) {
return addToResults(RELEASE_LEVEL_PATCH)
}
return results
},
[[], [], []] as [Commit[], Commit[], Commit[]],
)
const level = ifMinusOneToUndefined(
commitsByLevel.findIndex((commits) => commits.length > 0),
) as BumperRecommendation['level']
const reason =
level === undefined
? 'No commit needs to be released according to bump rules'
: 'These are the commits that triggered a release. The highest release level was chosen\n' +
commitsByLevel
.map(
(commits, level) =>
` - ${RELEASE_LEVEL_NAMES[level]}: ${commits.map((commit) => `"${commit.header}"`).join(', ')}`,
)
.join('\n')
return Promise.resolve({ level, reason })
}
export type ExtraConfig = Partial<{
gitRawCommitsOpts: { from: string; to: string }
}>
// https://github.com/conventional-changelog/conventional-changelog/blob/conventional-changelog-conventionalcommits-v8.0.0/packages/conventional-changelog-conventionalcommits/src/utils.js
function addBangNotes(commit: Commit) {
const breakingHeaderPattern = /^(\w*)(?:\((.*)\))?!: (.*)$/
const match = commit.header?.match(breakingHeaderPattern)
if (match && commit.notes.length === 0) {
const noteText = match[3] // the description of the change.
return [
{
title: 'BREAKING CHANGE',
text: noteText,
},
]
}
return commit.notes
}
// https://stackoverflow.com/a/52331580/3263250
type Unpacked<T> = T extends (infer U)[] ? U : T
type Commit = Unpacked<Parameters<Preset['whatBump']>[0]>
const ifMinusOneToUndefined = (n: number) => (n !== -1 ? n : undefined)
// https://github.com/conventional-changelog/conventional-changelog/blob/conventional-recommended-bump-v10.0.0/packages/conventional-recommended-bump/src/bumper.ts#L24-L28
const RELEASE_LEVEL_NAMES = ['major', 'minor', 'patch'] as const
const [RELEASE_LEVEL_MAJOR, RELEASE_LEVEL_MINOR, RELEASE_LEVEL_PATCH] = [
...RELEASE_LEVEL_NAMES.keys(),
]
// noinspection JSUnusedGlobalSymbols
export default createReleaseItConfig()