-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
version.js
84 lines (63 loc) · 2.36 KB
/
version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import semver from 'semver';
import {readPackageUp} from 'read-pkg-up';
const {packageJson: pkg} = await readPackageUp();
export default class Version {
constructor(version) {
this.version = version;
}
isPrerelease() {
return Boolean(semver.prerelease(this.version));
}
satisfies(range) {
Version.validate(this.version);
return semver.satisfies(this.version, range, {
includePrerelease: true,
});
}
getNewVersionFrom(input) {
Version.validate(this.version);
if (!Version.isValidInput(input)) {
throw new Error(`Version should be either ${Version.SEMVER_INCREMENTS.join(', ')}, or a valid semver version.`);
}
return Version.SEMVER_INCREMENTS.includes(input) ? semver.inc(this.version, input) : input;
}
isGreaterThanOrEqualTo(otherVersion) {
Version.validate(this.version);
Version.validate(otherVersion);
return semver.gte(otherVersion, this.version);
}
isLowerThanOrEqualTo(otherVersion) {
Version.validate(this.version);
Version.validate(otherVersion);
return semver.lte(otherVersion, this.version);
}
static SEMVER_INCREMENTS = ['patch', 'minor', 'major', 'prepatch', 'preminor', 'premajor', 'prerelease'];
static PRERELEASE_VERSIONS = ['prepatch', 'preminor', 'premajor', 'prerelease'];
static isPrereleaseOrIncrement = input => new Version(input).isPrerelease() || Version.PRERELEASE_VERSIONS.includes(input);
static isValidVersion = input => Boolean(semver.valid(input));
static isValidInput = input => Version.SEMVER_INCREMENTS.includes(input) || Version.isValidVersion(input);
static validate(version) {
if (!Version.isValidVersion(version)) {
throw new Error('Version should be a valid semver version.');
}
}
static verifyRequirementSatisfied(dependency, version) {
if (!pkg.engines?.node) {
throw new Error('Please include a `engines.node` field in your package.json');
}
const versionRange = pkg.engines?.[dependency];
if (!versionRange) {
return;
}
if (!new Version(version).satisfies(versionRange)) {
throw new Error(`Please upgrade to ${dependency}${versionRange}`);
}
}
static getAndValidateNewVersionFrom(input, version) {
const newVersion = new Version(version).getNewVersionFrom(input);
if (new Version(version).isLowerThanOrEqualTo(newVersion)) {
throw new Error(`New version \`${newVersion}\` should be higher than current version \`${version}\``);
}
return newVersion;
}
}