generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
parse-changelog.ts
165 lines (151 loc) · 5.21 KB
/
parse-changelog.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
import Changelog from './changelog';
import { ChangeCategory, unreleased } from './constants';
/**
* Truncate the given string at 80 characters.
*
* @param line - The string to truncate.
* @returns The truncated string.
*/
function truncated(line: string) {
return line.length > 80 ? `${line.slice(0, 80)}...` : line;
}
/**
* Returns whether the given string is recognized as a valid change category.
*
* @param category - The string to validate.
* @returns Whether the given string is a valid change category.
*/
function isValidChangeCategory(category: string): category is ChangeCategory {
return ChangeCategory[category as ChangeCategory] !== undefined;
}
/**
* Constructs a Changelog instance that represents the given changelog, which
* is parsed for release and change information.
*
* @param options - Options.
* @param options.changelogContent - The changelog to parse.
* @param options.repoUrl - The GitHub repository URL for the current project.
* @param options.tagPrefix - The prefix used in tags before the version number.
* @returns A changelog instance that reflects the changelog text provided.
*/
export function parseChangelog({
changelogContent,
repoUrl,
tagPrefix = 'v',
}: {
changelogContent: string;
repoUrl: string;
tagPrefix?: string;
}) {
const changelogLines = changelogContent.split('\n');
const changelog = new Changelog({ repoUrl, tagPrefix });
const unreleasedHeaderIndex = changelogLines.indexOf(`## [${unreleased}]`);
if (unreleasedHeaderIndex === -1) {
throw new Error(`Failed to find ${unreleased} header`);
}
const unreleasedLinkReferenceDefinition = changelogLines.findIndex((line) => {
return line.startsWith(`[${unreleased}]:`);
});
if (unreleasedLinkReferenceDefinition === -1) {
throw new Error(`Failed to find ${unreleased} link reference definition`);
}
const contentfulChangelogLines = changelogLines.slice(
unreleasedHeaderIndex + 1,
unreleasedLinkReferenceDefinition,
);
let mostRecentRelease: string;
let mostRecentCategory: ChangeCategory | undefined | null;
let currentChangeEntry: string | undefined;
/**
* Finalize a change entry, adding it to the changelog.
*
* This is required because change entries can span multiple lines.
*
* @param options - Options.
* @param options.removeTrailingNewline - Indicates that the trailing newline
* is not a part of the change description, and should therefore be removed.
*/
function finalizePreviousChange({
removeTrailingNewline = false,
}: {
removeTrailingNewline?: boolean;
} = {}) {
if (!currentChangeEntry) {
return;
}
// This should never happen in practice, because `mostRecentCategory` is
// guaranteed to be set if `currentChangeEntry` is set.
/* istanbul ignore next */
if (!mostRecentCategory) {
throw new Error('Cannot finalize change without most recent category.');
}
if (removeTrailingNewline && currentChangeEntry.endsWith('\n')) {
currentChangeEntry = currentChangeEntry.slice(
0,
currentChangeEntry.length - 1,
);
}
changelog.addChange({
addToStart: false,
category: mostRecentCategory,
description: currentChangeEntry,
version: mostRecentRelease,
});
currentChangeEntry = undefined;
}
for (const line of contentfulChangelogLines) {
if (line.startsWith('## [')) {
const results = line.match(
/^## \[(\d+\.\d+\.\d+)\](?: - (\d\d\d\d-\d\d-\d\d))?(?: \[(\w+)\])?/u,
);
if (results === null) {
throw new Error(`Malformed release header: '${truncated(line)}'`);
}
// Trailing newline removed because the release section is expected to
// be prefixed by a newline.
finalizePreviousChange({
removeTrailingNewline: true,
});
mostRecentRelease = results[1];
mostRecentCategory = undefined;
const date = results[2];
const status = results[3];
changelog.addRelease({
addToStart: false,
date,
status,
version: mostRecentRelease,
});
} else if (line.startsWith('### ')) {
const results = line.match(/^### (\w+)$\b/u);
if (results === null) {
throw new Error(`Malformed category header: '${truncated(line)}'`);
}
const isFirstCategory = mostRecentCategory === null;
finalizePreviousChange({
removeTrailingNewline: !isFirstCategory,
});
if (!isValidChangeCategory(results[1])) {
throw new Error(`Invalid change category: '${results[1]}'`);
}
mostRecentCategory = results[1];
} else if (line.startsWith('- ')) {
if (!mostRecentCategory) {
throw new Error(`Category missing for change: '${truncated(line)}'`);
}
const description = line.slice(2);
finalizePreviousChange();
currentChangeEntry = description;
} else if (currentChangeEntry) {
currentChangeEntry += `\n${line}`;
} else if (line === '') {
continue;
} else {
throw new Error(`Unrecognized line: '${truncated(line)}'`);
}
}
// Trailing newline removed because the reference link definition section is
// expected to be separated by a newline.
finalizePreviousChange({ removeTrailingNewline: true });
return changelog;
}