Skip to content

Commit 454547b

Browse files
committed
Allow configuring 'currentVersion'
Rather than always obtaining the 'currentVersion' of the project from an environment variable, it can now be passed in via a CLI flag instead. Additionally, the validation for the version has been improved. It will now throw if the current version isn't a valid SemVer version, as our update logic depends upon it being valid. We also now ensure that the error message for a messing version is only printed when it was strictly required (it wasn't used for non-RC updates), and that the script terminates at that point. Closes #10
1 parent 07147af commit 454547b

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/cli.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable node/no-process-exit */
33

44
const { promises: fs, constants: fsConstants } = require('fs');
5+
const semver = require('semver');
56
const yargs = require('yargs/yargs');
67
const { hideBin } = require('yargs/helpers');
78

@@ -38,6 +39,12 @@ async function main() {
3839
description: 'The changelog file path',
3940
type: 'string',
4041
})
42+
.option('currentVersion', {
43+
default: npmPackageVersion,
44+
description:
45+
'The current version of the project that the changelog belongs to.',
46+
type: 'string',
47+
})
4148
.epilog(updateEpilog),
4249
)
4350
.strict()
@@ -47,19 +54,26 @@ async function main() {
4754
`Utilities for validating and updating "Keep a Changelog" formatted changelogs.\nUsage: $0 [command] [options]`,
4855
);
4956

50-
if (!npmPackageVersion) {
57+
const {
58+
currentVersion,
59+
file: changelogFilename,
60+
rc: isReleaseCandidate,
61+
} = argv;
62+
63+
if (isReleaseCandidate && !currentVersion) {
5164
console.error(
52-
`npm package version not found. Please run this as an npm script from a project with the 'version' field set.`,
65+
`Version not found. Please set the --currentVersion flag, or run this as an npm script from a project with the 'version' field set.`,
5366
);
67+
process.exit(1);
68+
} else if (currentVersion && semver.valid(currentVersion) === null) {
69+
console.error(`Current version is not valid SemVer: '${currentVersion}'`);
70+
process.exit(1);
5471
} else if (!npmPackageRepositoryUrl) {
5572
console.error(
5673
`npm package repository URL not found. Please run this as an npm script from a project with the 'repository' field set.`,
5774
);
5875
}
5976

60-
const isReleaseCandidate = argv.rc;
61-
const changelogFilename = argv.file;
62-
6377
try {
6478
// eslint-disable-next-line no-bitwise
6579
await fs.access(changelogFilename, fsConstants.F_OK | fsConstants.W_OK);
@@ -78,7 +92,7 @@ async function main() {
7892

7993
const newChangelogContent = await updateChangelog({
8094
changelogContent,
81-
currentVersion: npmPackageVersion,
95+
currentVersion,
8296
repoUrl: npmPackageRepositoryUrl,
8397
isReleaseCandidate,
8498
});

src/updateChangelog.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ function getAllLoggedPrNumbers(changelog) {
100100
* PRs that are already included in the changelog are omitted.
101101
* @param {Object} options
102102
* @param {string} options.changelogContent - The current changelog
103-
* @param {Version} options.currentVersion - The current version
103+
* @param {Version} [options.currentVersion] - The current version. Required if
104+
* `isReleaseCandidate` is set, but optional otherwise.
104105
* @param {string} options.repoUrl - The GitHub repository URL for the current
105106
* project.
106107
* @param {boolean} options.isReleaseCandidate - Denotes whether the current
@@ -115,6 +116,11 @@ async function updateChangelog({
115116
repoUrl,
116117
isReleaseCandidate,
117118
}) {
119+
if (isReleaseCandidate && !currentVersion) {
120+
throw new Error(
121+
`A version must be specified if 'isReleaseCandidate' is set.`,
122+
);
123+
}
118124
const changelog = parseChangelog({ changelogContent, repoUrl });
119125

120126
// Ensure we have all tags on remote

0 commit comments

Comments
 (0)