-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
changelog.config.js
218 lines (194 loc) · 5.1 KB
/
changelog.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
const { readFileSync } = require('fs');
const { resolve } = require('path');
const { tokens, packages } = require('./changelogData');
const PACKAGE_REGEXP = new RegExp(`${packages.join('|')}`);
const GROUPS = [
'Breaking Changes',
'Bug Fixes',
'Features',
'Documentation',
'Performance Improvements',
'Other Internal Changes',
];
const BREAKING_CHANGES = 'Breaking Changes';
/**
* @param commit {import("conventional-commits-parser").Commit}
*/
function getCommitType({ type, scope, revert }) {
if (type === 'revert' || revert) {
return 'Reverts';
}
if (scope === 'docs') {
return 'Documentation';
}
switch (type) {
case 'feat':
return 'Features';
case 'fix':
case 'bugfix':
return 'Bug Fixes';
case 'perf':
return 'Performance Improvements';
case 'docs':
return 'Documentation';
default:
return 'Other Internal Changes';
}
}
function getCommitScope({ scope }) {
if (PACKAGE_REGEXP.test(scope)) {
return `@react-md/${scope}`;
}
switch (scope) {
// these are mostly to create the initial changelog
case 'docs':
case 'pages':
case 'sassdoc':
case 'sandbox':
case 'demos':
case 'blog':
case 'indexer':
case 'guides':
case 'website':
return 'react-md.dev';
case 'slider':
return '@react-md/form';
case 'grid':
return '@react-md/utils';
default:
return scope || '';
}
}
const TOKEN_REGEXP = new RegExp(
`(?<=\\s|^)(${tokens.join('|')})(?=\\s|$)`,
'g'
);
function tokenize(subject) {
return subject
.replace(TOKEN_REGEXP, '`$1`')
.replace(/([a-z][A-z]+Props)/g, '`$1`');
}
/**
* @type {import("conventional-changelog-core").ParserOptions}
*/
const parserOpts = {
headerPattern: /^(\w*)(?:\((.*)\))?: (.*)$/,
headerCorrespondence: ['type', 'scope', 'subject'],
noteKeywords: ['BREAKING CHANGE'],
revertPattern:
/^(?:Revert|revert:)\s"?([\s\S]+?)"?\s*This reverts commit (\w*)\./i,
revertCorrespondence: ['header', 'hash'],
};
/**
* @type {import("conventional-changelog-core").WriterOptions}
*/
const writerOpts = {
transform: (commit, context) => {
// don't want to show the release tag in changelog
if (commit.scope === 'release') {
return;
}
const issues = [];
let isBreaking = false;
commit.notes.forEach((note) => {
isBreaking = true;
note.title = BREAKING_CHANGES;
});
commit.type = getCommitType(commit);
commit.scope = getCommitScope(commit);
if (!isBreaking && (!commit.type || commit.scope.includes('deps'))) {
// don't include un-typed commits in changelogs or deps
return;
}
if (typeof commit.hash === 'string') {
commit.shortHash = commit.hash.substring(0, 7);
}
if (typeof commit.subject === 'string') {
let url = context.repository
? `${context.host}/${context.owner}/${context.repository}`
: context.repoUrl;
if (url) {
url = `${url}/issues/`;
commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
issues.push(issue);
return `[#${issue}](${url}${issue})`;
});
}
commit.subject = tokenize(commit.subject);
}
// remove references that already appear in the subject
commit.references = commit.references.filter(
(reference) => !issues.includes(reference.issue)
);
return commit;
},
groupBy: 'type',
commitGroupsSort: function (a, b) {
const aIndex = GROUPS.indexOf(a.title);
const bIndex = GROUPS.indexOf(b.title);
if (aIndex !== -1 && bIndex !== -1) {
return aIndex - bIndex;
}
return a.title.localeCompare(b.title);
},
commitsSort: ['scope', 'subject'],
noteGroupsSort: 'title',
mainTemplate: readFileSync(
resolve(__dirname, './templates/template.hbs'),
'utf-8'
),
headerPartial: readFileSync(
resolve(__dirname, './templates/header.hbs'),
'utf-8'
),
commitPartial: readFileSync(
resolve(__dirname, './templates/commit.hbs'),
'utf-8'
),
footerPartial: readFileSync(
resolve(__dirname, './templates/footer.hbs'),
'utf-8'
),
};
/**
* This is basically the conventional-changelog-angular with a few changes to
* allow more commit messages to appear. I also "tokenize" known packages and
* exports from react-md in the changelogs.
*
* @type {import("conventional-changelog-core").Options.Config}
*/
module.exports = {
parserOpts,
writerOpts,
conventionalChangelog: {
writerOpts,
parserOpts,
},
recommendedBumpOpts: {
config: {
parserOpts,
writerOpts,
},
whatBump: (commits) => {
let level = 2;
let breaking = 0;
let features = 0;
commits.forEach((commit) => {
if (commit.notes.length > 0) {
breaking += commit.notes.length;
level = 0;
} else if (commit.type === 'feat') {
features += 1;
if (level === 2) {
level = 1;
}
}
});
const verb = breaking === 1 ? 'is' : 'are';
return {
level,
reason: `There ${verb} ${breaking} BREAKING CHANGES and ${features} features`,
};
},
},
};