diff --git a/src/update-changelog.ts b/src/update-changelog.ts index f9d6a57..14d0658 100644 --- a/src/update-changelog.ts +++ b/src/update-changelog.ts @@ -197,11 +197,6 @@ export async function updateChangelog({ tagPrefixes = ['v'], formatter = undefined, }: UpdateChangelogOptions): Promise { - if (isReleaseCandidate && !currentVersion) { - throw new Error( - `A version must be specified if 'isReleaseCandidate' is set.`, - ); - } const changelog = parseChangelog({ changelogContent, repoUrl, @@ -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( @@ -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();