Skip to content

Commit

Permalink
Fix type narrowing for currentVersion by consolidating conditional …
Browse files Browse the repository at this point in the history
…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
1 parent 8da9f21 commit 033cd4e
Showing 1 changed file with 36 additions and 38 deletions.
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.`,
);
}

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

0 comments on commit 033cd4e

Please sign in to comment.