Skip to content

Commit

Permalink
Show new version in non-interactive mode (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Mar 30, 2023
1 parent f992ec1 commit a0a17fd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
10 changes: 1 addition & 9 deletions source/prerequisite-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,7 @@ module.exports = (input, pkg, options) => {
{
title: 'Validate version',
task: () => {
if (!version.isValidInput(input)) {
throw new Error(`Version should be either ${version.SEMVER_INCREMENTS.join(', ')}, or a valid semver version.`);
}

newVersion = version(pkg.version).getNewVersionFrom(input);

if (version(pkg.version).isLowerThanOrEqualTo(newVersion)) {
throw new Error(`New version \`${newVersion}\` should be higher than current version \`${pkg.version}\``);
}
newVersion = version.getAndValidateNewVersionFrom(input, pkg.version);
}
},
{
Expand Down
5 changes: 4 additions & 1 deletion source/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ module.exports = async (options, pkg) => {
if (options.releaseDraftOnly) {
console.log(`\nCreate a release draft on GitHub for ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
} else {
console.log(`\nPublish a new version of ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
const newVersion = options.version ? version.getAndValidateNewVersionFrom(options.version, oldVersion) : undefined;
const versionText = chalk.dim(`(current: ${oldVersion}${newVersion ? `, next: ${prettyVersionDiff(oldVersion, newVersion)}` : ''}${chalk.dim(')')}`);

console.log(`\nPublish a new version of ${chalk.bold.magenta(pkg.name)} ${versionText}\n`);
}

const prompts = [
Expand Down
12 changes: 11 additions & 1 deletion source/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Version {
getNewVersionFrom(input) {
module.exports.validate(this.version);
if (!module.exports.isValidInput(input)) {
throw new Error(`Version should be either ${module.exports.SEMVER_INCREMENTS.join(', ')} or a valid semver version.`);
throw new Error(`Version should be either ${module.exports.SEMVER_INCREMENTS.join(', ')}, or a valid semver version.`);
}

return module.exports.SEMVER_INCREMENTS.includes(input) ? semver.inc(this.version, input) : input;
Expand Down Expand Up @@ -64,3 +64,13 @@ module.exports.verifyRequirementSatisfied = (dependency, version) => {
throw new Error(`Please upgrade to ${dependency}${depRange}`);
}
};

module.exports.getAndValidateNewVersionFrom = (input, version) => {
const newVersion = module.exports(version).getNewVersionFrom(input);

if (module.exports(version).isLowerThanOrEqualTo(newVersion)) {
throw new Error(`New version \`${newVersion}\` should be higher than current version \`${version}\``);
}

return newVersion;
};
21 changes: 20 additions & 1 deletion test/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test('version.isPrereleaseOrIncrement', t => {
});

test('version.getNewVersionFrom', t => {
const message = 'Version should be either patch, minor, major, prepatch, preminor, premajor, prerelease or a valid semver version.';
const message = 'Version should be either patch, minor, major, prepatch, preminor, premajor, prerelease, or a valid semver version.';

t.throws(() => version('1.0.0').getNewVersionFrom('patchxxx'), message);
t.throws(() => version('1.0.0').getNewVersionFrom('1.0.0.0'), message);
Expand Down Expand Up @@ -118,3 +118,22 @@ test('version.satisfies', t => {
t.false(version('3.0.0').satisfies('>=2.15.8 <3.0.0 || >=3.10.1'));
t.false(version('3.10.0').satisfies('>=2.15.8 <3.0.0 || >=3.10.1'));
});

test('version.getAndValidateNewVersionFrom', t => {
t.is(version.getAndValidateNewVersionFrom('patch', '1.0.0'), '1.0.1');

t.throws(
() => version.getAndValidateNewVersionFrom('patch', '1'),
'Version should be a valid semver version.'
);

t.throws(
() => version.getAndValidateNewVersionFrom('lol', '1.0.0'),
`Version should be either ${version.SEMVER_INCREMENTS.join(', ')}, or a valid semver version.`
);

t.throws(
() => version.getAndValidateNewVersionFrom('1.0.0', '2.0.0'),
'New version `1.0.0` should be higher than current version `2.0.0`'
);
});

0 comments on commit a0a17fd

Please sign in to comment.