smartversion/ts/index.ts

83 lines
2.2 KiB
TypeScript
Raw Normal View History

import * as plugins from './smartversion.plugins';
2017-08-17 14:15:47 +00:00
export class SmartVersion {
2021-04-26 07:16:26 +00:00
public static fromFuzzyString(fuzzyString): SmartVersion {
return new SmartVersion(plugins.semver.minVersion(fuzzyString).version, fuzzyString);
}
public originalVersionString: string;
public semver: plugins.semver.SemVer;
2019-09-11 15:42:34 +00:00
public versionString: string;
public update = {
2017-08-17 14:15:47 +00:00
patch: () => {
this.semver.patch = this.semver.patch + 1;
2017-08-17 14:15:47 +00:00
},
minor: () => {
this.semver.minor = this.semver.minor + 1;
2017-08-17 14:15:47 +00:00
},
major: () => {
this.semver.major = this.semver.major + 1;
2021-04-25 08:52:39 +00:00
},
};
2017-08-17 14:15:47 +00:00
2021-04-26 07:16:26 +00:00
constructor(semVerStringArg: string, originalStringArg?: string) {
this.originalVersionString = originalStringArg;
this.semver = new plugins.semver.SemVer(semVerStringArg);
this.versionString = this.semver.version;
2017-08-17 14:15:47 +00:00
}
2019-09-11 15:42:34 +00:00
public get major() {
return this.semver.major;
2017-08-17 14:15:47 +00:00
}
2019-09-11 15:42:34 +00:00
public get minor() {
return this.semver.minor;
2017-08-17 14:15:47 +00:00
}
2019-09-11 15:42:34 +00:00
public get patch() {
return this.semver.patch;
2017-08-17 14:15:47 +00:00
}
2019-09-11 15:42:34 +00:00
public greaterThan(smartVersionArg: SmartVersion) {
return this.greaterThanString(smartVersionArg.versionString);
}
/**
* compares the version of this against a string
*/
2019-09-11 15:42:34 +00:00
public greaterThanString(versionStringArg) {
return plugins.semver.gt(this.versionString, versionStringArg);
2017-08-18 08:58:00 +00:00
}
2019-09-11 15:42:34 +00:00
public lessThan(smartVersionArg: SmartVersion) {
return this.lessThanString(smartVersionArg.versionString);
}
/**
* compares the version of this against a string
*/
2019-09-11 15:42:34 +00:00
public lessThanString(versionStringArg) {
return plugins.semver.lt(this.versionString, versionStringArg);
2017-08-18 08:58:00 +00:00
}
2021-04-26 07:16:26 +00:00
/**
* tries to get the best match from a range of available versions
*/
public getBestMatch(availableVersions: string[]): string {
let bestMatchingVersion: string;
for (const versionArg of availableVersions) {
if (!plugins.semver.satisfies(versionArg, this.originalVersionString)) {
continue;
}
if(!bestMatchingVersion) {
bestMatchingVersion = versionArg;
} else {
if (plugins.semver.lt(bestMatchingVersion, versionArg)) {
bestMatchingVersion = versionArg;
}
}
}
return bestMatchingVersion;
}
2017-08-17 14:15:47 +00:00
}