Skip to content

Commit

Permalink
Add --fix option to validate command (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz authored Jun 23, 2023
1 parent f9c09aa commit aa3207a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type ValidateOptions = {
isReleaseCandidate: boolean;
repoUrl: string;
tagPrefix: string;
fix: boolean;
};

/**
Expand All @@ -145,13 +146,15 @@ type ValidateOptions = {
* @param options.isReleaseCandidate - Whether the current branch is a release candidate or not.
* @param options.repoUrl - The GitHub repository URL for the current project.
* @param options.tagPrefix - The prefix used in tags before the version number.
* @param options.fix - Whether to attempt to fix the changelog or not.
*/
async function validate({
changelogPath,
currentVersion,
isReleaseCandidate,
repoUrl,
tagPrefix,
fix,
}: ValidateOptions) {
const changelogContent = await readChangelog(changelogPath);

Expand All @@ -167,6 +170,11 @@ async function validate({
} catch (error) {
if (error instanceof ChangelogFormattingError) {
const { validChangelog, invalidChangelog } = error.data;
if (fix) {
await saveChangelog(changelogPath, validChangelog);
return undefined;
}

const diff = generateDiff(validChangelog, invalidChangelog);
return exitWithError(`Changelog not well-formatted. Diff:\n\n${diff}`);
} else if (error instanceof InvalidChangelogError) {
Expand Down Expand Up @@ -279,6 +287,11 @@ async function main() {
'The current version of the project that the changelog belongs to.',
type: 'string',
})
.option('fix', {
default: false,
description: `Attempt to fix any formatting errors in the changelog`,
type: 'boolean',
})
.epilog(validateEpilog),
)
.command('init', 'Initialize a new empty changelog', (_yargs) => {
Expand All @@ -297,6 +310,7 @@ async function main() {
repo: repoUrl,
root: projectRootDirectory,
tagPrefix,
fix,
} = argv;
let { currentVersion } = argv;

Expand Down Expand Up @@ -411,6 +425,7 @@ async function main() {
isReleaseCandidate,
repoUrl,
tagPrefix,
fix,
});
} else if (command === 'init') {
await init({
Expand Down

0 comments on commit aa3207a

Please sign in to comment.