Skip to content
Prev Previous commit
Next Next commit
Fix type narrowing for currentVersion by consolidating conditional …
…branches

- more consistent logic for `isReleaseCandidate` null check
- wasted processing overhead due to errors throwing later than strictly necessary
  • Loading branch information
MajorLift committed Oct 16, 2023
commit 033cd4e3f119bbeffec9f1c942e44a7549c6b7a3
74 changes: 36 additions & 38 deletions src/update-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,6 @@ export async function updateChangelog({
tagPrefixes = ['v'],
formatter = undefined,
}: UpdateChangelogOptions): Promise<string | undefined> {
if (isReleaseCandidate && !currentVersion) {
throw new Error(
`A version must be specified if 'isReleaseCandidate' is set.`,
);
}
const changelog = parseChangelog({
changelogContent,
repoUrl,
Expand All @@ -215,15 +210,6 @@ export async function updateChangelog({
tagPrefixes,
});

if (
isReleaseCandidate &&
mostRecentTag === `${tagPrefixes[0]}${currentVersion ?? ''}`
) {
throw new Error(
`Current version already has tag, which is unexpected for a release candidate.`,
);
}

const commitRange =
mostRecentTag === null ? 'HEAD' : `${mostRecentTag}..HEAD`;
const commitsHashesSinceLastRelease = await getCommitHashesInRange(
Expand All @@ -247,35 +233,47 @@ export async function updateChangelog({
return undefined;
}

// Ensure release header exists, if necessary
if (
isReleaseCandidate &&
currentVersion &&
!changelog
.getReleases()
.find((release) => release.version === currentVersion)
) {
changelog.addRelease({ version: currentVersion });
}
if (isReleaseCandidate) {
if (!currentVersion) {
throw new Error(
`A version must be specified if 'isReleaseCandidate' is set.`,
);
}

if (isReleaseCandidate && currentVersion && hasUnreleasedChanges) {
changelog.migrateUnreleasedChangesToRelease(currentVersion);
}
if (mostRecentTag === `${tagPrefixes[0]}${currentVersion ?? ''}`) {
throw new Error(
`Current version already has tag, which is unexpected for a release candidate.`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about, adding the tag value part of the error message?

Copy link
Contributor Author

@MajorLift MajorLift Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean something like this?

Suggested change
`Current version already has tag, which is unexpected for a release candidate.`,
`Current version already has a tag: '${mostRecentTag}'. This is unexpected for a release candidate.`,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kanthesha Let me know if a26b0bb looks good!

);
}

const newChangeEntries = newCommits.map(({ prNumber, description }) => {
if (prNumber) {
const suffix = `([#${prNumber}](${repoUrl}/pull/${prNumber}))`;
return `${description} ${suffix}`;
// Ensure release header exists, if necessary
if (
!changelog
.getReleases()
.find((release) => release.version === currentVersion)
) {
changelog.addRelease({ version: currentVersion });
}
return description;
});

for (const description of newChangeEntries.reverse()) {
changelog.addChange({
version: isReleaseCandidate ? currentVersion : undefined,
category: ChangeCategory.Uncategorized,
description,
if (hasUnreleasedChanges) {
changelog.migrateUnreleasedChangesToRelease(currentVersion);
}

const newChangeEntries = newCommits.map(({ prNumber, description }) => {
if (prNumber) {
const suffix = `([#${prNumber}](${repoUrl}/pull/${prNumber}))`;
return `${description} ${suffix}`;
}
return description;
});

for (const description of newChangeEntries.reverse()) {
changelog.addChange({
version: isReleaseCandidate ? currentVersion : undefined,
category: ChangeCategory.Uncategorized,
description,
});
}
}

return changelog.toString();
Expand Down