Fix a bug with getting the highest bump type

This commit is contained in:
mitchellhamilton
2019-09-11 15:49:39 +10:00
parent 7d4dde44b2
commit 8db2bfe48d
9 changed files with 9754 additions and 548 deletions
+68
View File
@@ -0,0 +1,68 @@
import unified from "unified";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
// @ts-ignore
import mdastToString from "mdast-util-to-string";
export const BumpLevels = {
dep: 0,
patch: 1,
minor: 2,
major: 3
} as const;
export function getChangelogEntry(changelog: string, version: string) {
let ast = unified()
.use(remarkParse)
.parse(changelog);
let highestLevel: number = BumpLevels.dep;
let nodes = ast.children as Array<any>;
let headingStartInfo:
| {
index: number;
depth: number;
}
| undefined;
let endIndex: number | undefined;
for (let i = 0; i < nodes.length; i++) {
let node = nodes[i];
if (node.type === "heading") {
let stringified: string = mdastToString(node);
let match = stringified.toLowerCase().match(/(major|minor|patch)/);
if (match !== null) {
let level = BumpLevels[match[0] as "major" | "minor" | "patch"];
highestLevel = Math.max(level, highestLevel);
}
if (headingStartInfo === undefined && stringified === version) {
headingStartInfo = {
index: i,
depth: node.depth
};
continue;
}
if (
endIndex === undefined &&
headingStartInfo !== undefined &&
headingStartInfo.depth === node.depth
) {
endIndex = i;
break;
}
}
}
if (headingStartInfo) {
ast.children = (ast.children as any).slice(
headingStartInfo.index + 1,
endIndex
);
}
return {
content: unified()
.use(remarkStringify)
.stringify(ast),
highestLevel: highestLevel
};
}