fix(cli): improve changelog release handling and TypeScript compatibility
This commit is contained in:
+22
-8
@@ -19,6 +19,12 @@ export interface IPendingChangelog {
|
||||
isEmpty: boolean;
|
||||
}
|
||||
|
||||
interface IChangelogSection {
|
||||
start: number;
|
||||
bodyStart: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
const bucketForCommitType = (commitType: string): TChangelogBucket => {
|
||||
switch (commitType) {
|
||||
case "BREAKING CHANGE":
|
||||
@@ -48,7 +54,7 @@ const writeChangelog = async (filePath: string, content: string): Promise<void>
|
||||
const findPendingSection = (
|
||||
content: string,
|
||||
sectionName: string,
|
||||
): { start: number; bodyStart: number; end: number } | null => {
|
||||
): IChangelogSection | null => {
|
||||
const headingRegex = new RegExp(`^##\\s+${sectionName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*$`, "m");
|
||||
const match = headingRegex.exec(content);
|
||||
if (!match || match.index === undefined) {
|
||||
@@ -123,9 +129,11 @@ export const readPendingChangelog = async (
|
||||
filePath: string,
|
||||
sectionName = "Pending",
|
||||
): Promise<IPendingChangelog> => {
|
||||
const content = await ensurePendingSection(filePath, sectionName);
|
||||
const pendingSection = findPendingSection(content, sectionName)!;
|
||||
const block = content.slice(pendingSection.bodyStart, pendingSection.end).trim();
|
||||
const content = await readChangelog(filePath);
|
||||
const pendingSection = findPendingSection(content, sectionName);
|
||||
const block = pendingSection
|
||||
? content.slice(pendingSection.bodyStart, pendingSection.end).trim()
|
||||
: "";
|
||||
return {
|
||||
block,
|
||||
isEmpty: block.length === 0,
|
||||
@@ -149,8 +157,11 @@ export const movePendingToVersion = async (
|
||||
version: string,
|
||||
dateString: string,
|
||||
): Promise<void> => {
|
||||
let content = await ensurePendingSection(filePath, sectionName);
|
||||
const pendingSection = findPendingSection(content, sectionName)!;
|
||||
let content = await readChangelog(filePath);
|
||||
const pendingSection = findPendingSection(content, sectionName);
|
||||
if (!pendingSection) {
|
||||
throw new Error("No pending changelog entries. Nothing to release.");
|
||||
}
|
||||
const pendingBlock = content.slice(pendingSection.bodyStart, pendingSection.end).trim();
|
||||
if (!pendingBlock) {
|
||||
throw new Error("No pending changelog entries. Nothing to release.");
|
||||
@@ -159,7 +170,10 @@ export const movePendingToVersion = async (
|
||||
const renderedHeading = versionHeading
|
||||
.replaceAll("{{version}}", version)
|
||||
.replaceAll("{{date}}", dateString);
|
||||
const nextContent = content.slice(pendingSection.end).replace(/^\n+/, "");
|
||||
content = `${content.slice(0, pendingSection.bodyStart)}\n\n${renderedHeading}\n\n${pendingBlock}\n\n${nextContent}`;
|
||||
const beforePending = content.slice(0, pendingSection.start).trimEnd();
|
||||
const afterPending = content.slice(pendingSection.end).replace(/^\n+/, "").trimEnd();
|
||||
content = [beforePending, renderedHeading, pendingBlock, afterPending]
|
||||
.filter((block) => block.length > 0)
|
||||
.join("\n\n");
|
||||
await writeChangelog(filePath, content);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user