Skip to content

Commit

Permalink
version style tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Sep 1, 2023
1 parent f98b85d commit c6249a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions source/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ const ui = async (options, {pkg, rootDir}) => {
when: answers => answers.version === undefined,
filter(input) {
if (SEMVER_INCREMENTS.includes(input)) {
throw new Error('Custom version should not be a `SemVer` increment.');
throw new Error('Custom version should not be a SemVer increment.');
}

const version = new Version(oldVersion);
Expand All @@ -252,7 +252,7 @@ const ui = async (options, {pkg, rootDir}) => {
version.setFrom(input);
} catch (error) {
if (error.message.includes('valid `SemVer` version')) {
throw new Error(`Custom version \`${input}\` should be a valid \`SemVer\` version.`);
throw new Error(`Custom version ${input} should be a valid SemVer version.`);
}

error.message = error.message.replace('New', 'Custom');
Expand Down
16 changes: 8 additions & 8 deletions source/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {template as chalk} from 'chalk-template';

/** @type {string[]} Allowed `SemVer` release types. */
export const SEMVER_INCREMENTS = semver.RELEASE_TYPES.sort();
export const SEMVER_INCREMENTS_LIST = `\`${SEMVER_INCREMENTS.join('`, `')}\``;
const SEMVER_INCREMENTS_LIST_LAST_OR = `\`${SEMVER_INCREMENTS.slice(0, -1).join('`, `')}\`, or \`${SEMVER_INCREMENTS.slice(-1)}\``;
export const SEMVER_INCREMENTS_LIST = SEMVER_INCREMENTS.join(', ');
const SEMVER_INCREMENTS_LIST_LAST_OR = `${SEMVER_INCREMENTS.slice(0, -1).join(', ')}, or ${SEMVER_INCREMENTS.slice(-1)}`;

/** @typedef {semver.SemVer} SemVerInstance */
/** @typedef {semver.ReleaseType} SemVerIncrement */
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class Version {
this.#version = semver.parse(version);

if (this.#version === null) {
throw new Error(`Version \`${version}\` should be a valid \`SemVer\` version.`);
throw new Error(`Version ${version} should be a valid SemVer version.`);
}
}

Expand All @@ -67,7 +67,7 @@ export default class Version {

if (increment) {
if (!isSemVerIncrement(increment)) {
throw new Error(`Increment \`${increment}\` should be one of ${SEMVER_INCREMENTS_LIST_LAST_OR}.`);
throw new Error(`Increment ${increment} should be one of ${SEMVER_INCREMENTS_LIST_LAST_OR}.`);
}

this.setFrom(increment);
Expand All @@ -90,11 +90,11 @@ export default class Version {
this.#version.inc(input, this.#prereleasePrefix);
} else {
if (isInvalidSemVerVersion(input)) {
throw new Error(`New version \`${input}\` should either be one of ${SEMVER_INCREMENTS_LIST}, or a valid \`SemVer\` version.`);
throw new Error(`New version ${input} should either be one of ${SEMVER_INCREMENTS_LIST}, or a valid SemVer version.`);
}

if (this.#isGreaterThanOrEqualTo(input)) {
throw new Error(`New version \`${input}\` should be higher than current version \`${this.toString()}\`.`);
throw new Error(`New version ${input} should be higher than current version ${this.toString()}.`);
}

this.#trySetVersion(input);
Expand All @@ -121,7 +121,7 @@ export default class Version {
const previousSemver = semver.parse(previousVersion);

if (previousSemver === null) {
throw new Error(`Previous version \`${previousVersion}\` should be a valid \`SemVer\` version.`);
throw new Error(`Previous version ${previousVersion} should be a valid SemVer version.`);
}

previousVersion = previousSemver;
Expand Down Expand Up @@ -164,7 +164,7 @@ export default class Version {
*/
satisfies(range) {
if (!semver.validRange(range)) {
throw new Error(`Range \`${range}\` is not a valid \`SemVer\` range.`);
throw new Error(`Range ${range} is not a valid SemVer range.`);
}

return semver.satisfies(this.#version, range, {
Expand Down
20 changes: 10 additions & 10 deletions test/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {template as chalk} from 'chalk-template';
import semver from 'semver';
import Version from '../source/version.js';

const INCREMENT_LIST = '`major`, `minor`, `patch`, `premajor`, `preminor`, `prepatch`, `prerelease`';
const INCREMENT_LIST_OR = '`major`, `minor`, `patch`, `premajor`, `preminor`, `prepatch`, or `prerelease`';
const INCREMENT_LIST = 'major, minor, patch, premajor, preminor, prepatch, prerelease';
const INCREMENT_LIST_OR = 'major, minor, patch, premajor, preminor, prepatch, or prerelease';

/** @param {string} input - Place `{ }` around the version parts to be highlighted. */
const makeNewFormattedVersion = input => {
Expand All @@ -20,7 +20,7 @@ test('new Version - valid', t => {
test('new Version - invalid', t => {
t.throws(
() => new Version('major'),
{message: 'Version `major` should be a valid `SemVer` version.'},
{message: 'Version major should be a valid SemVer version.'},
);
});

Expand All @@ -31,21 +31,21 @@ test('new Version - valid w/ valid increment', t => {
test('new Version - invalid w/ valid increment', t => {
t.throws(
() => new Version('major', 'major'),
{message: 'Version `major` should be a valid `SemVer` version.'},
{message: 'Version major should be a valid SemVer version.'},
);
});

test('new Version - valid w/ invalid increment', t => {
t.throws(
() => new Version('1.0.0', '2.0.0'),
{message: `Increment \`2.0.0\` should be one of ${INCREMENT_LIST_OR}.`},
{message: `Increment 2.0.0 should be one of ${INCREMENT_LIST_OR}.`},
);
});

test('new Version - invalid w/ invalid increment', t => {
t.throws(
() => new Version('major', '2.0.0'),
{message: 'Version `major` should be a valid `SemVer` version.'},
{message: 'Version major should be a valid SemVer version.'},
);
});

Expand All @@ -57,14 +57,14 @@ test('setFrom - valid input as version', t => {
test('setFrom - invalid input as version', t => {
t.throws(
() => new Version('1.0.0').setFrom('200'),
{message: `New version \`200\` should either be one of ${INCREMENT_LIST}, or a valid \`SemVer\` version.`},
{message: `New version 200 should either be one of ${INCREMENT_LIST}, or a valid SemVer version.`},
);
});

test('setFrom - valid input is not higher than version', t => {
t.throws(
() => new Version('1.0.0').setFrom('0.2.0'),
{message: 'New version `0.2.0` should be higher than current version `1.0.0`.'},
{message: 'New version 0.2.0 should be higher than current version 1.0.0.'},
);
});

Expand Down Expand Up @@ -199,7 +199,7 @@ test('format - previousVersion as SemVer instance', t => {
test('format - invalid previousVersion', t => {
t.throws(
() => new Version('1.0.0').format({previousVersion: '000'}),
{message: 'Previous version `000` should be a valid `SemVer` version.'},
{message: 'Previous version 000 should be a valid SemVer version.'},
);
});

Expand All @@ -214,7 +214,7 @@ test('satisfies', t => {

t.throws(
() => new Version('1.2.3').satisfies('=>1.0.0'),
{message: 'Range `=>1.0.0` is not a valid `SemVer` range.'},
{message: 'Range =>1.0.0 is not a valid SemVer range.'},
);
});

Expand Down

0 comments on commit c6249a2

Please sign in to comment.