Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable node/no-process-exit */

const { promises: fs, constants: fsConstants } = require('fs');
const semver = require('semver');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

Expand Down Expand Up @@ -38,6 +39,12 @@ async function main() {
description: 'The changelog file path',
type: 'string',
})
.option('currentVersion', {
default: npmPackageVersion,
description:
'The current version of the project that the changelog belongs to.',
type: 'string',
})
.epilog(updateEpilog),
)
.strict()
Expand All @@ -47,19 +54,26 @@ async function main() {
`Utilities for validating and updating "Keep a Changelog" formatted changelogs.\nUsage: $0 [command] [options]`,
);

if (!npmPackageVersion) {
const {
currentVersion,
file: changelogFilename,
rc: isReleaseCandidate,
} = argv;

if (isReleaseCandidate && !currentVersion) {
console.error(
`npm package version not found. Please run this as an npm script from a project with the 'version' field set.`,
`Version not found. Please set the --currentVersion flag, or run this as an npm script from a project with the 'version' field set.`,
);
process.exit(1);
} else if (currentVersion && semver.valid(currentVersion) === null) {
console.error(`Current version is not valid SemVer: '${currentVersion}'`);
process.exit(1);
} else if (!npmPackageRepositoryUrl) {
console.error(
`npm package repository URL not found. Please run this as an npm script from a project with the 'repository' field set.`,
);
}

const isReleaseCandidate = argv.rc;
const changelogFilename = argv.file;

try {
// eslint-disable-next-line no-bitwise
await fs.access(changelogFilename, fsConstants.F_OK | fsConstants.W_OK);
Expand All @@ -78,7 +92,7 @@ async function main() {

const newChangelogContent = await updateChangelog({
changelogContent,
currentVersion: npmPackageVersion,
currentVersion,
repoUrl: npmPackageRepositoryUrl,
isReleaseCandidate,
});
Expand Down
8 changes: 7 additions & 1 deletion src/updateChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ function getAllLoggedPrNumbers(changelog) {
* PRs that are already included in the changelog are omitted.
* @param {Object} options
* @param {string} options.changelogContent - The current changelog
* @param {Version} options.currentVersion - The current version
* @param {Version} [options.currentVersion] - The current version. Required if
* `isReleaseCandidate` is set, but optional otherwise.
* @param {string} options.repoUrl - The GitHub repository URL for the current
* project.
* @param {boolean} options.isReleaseCandidate - Denotes whether the current
Expand All @@ -115,6 +116,11 @@ async function updateChangelog({
repoUrl,
isReleaseCandidate,
}) {
if (isReleaseCandidate && !currentVersion) {
throw new Error(
`A version must be specified if 'isReleaseCandidate' is set.`,
);
}
const changelog = parseChangelog({ changelogContent, repoUrl });

// Ensure we have all tags on remote
Expand Down