smartversion/ts/index.ts

101 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-04-26 12:44:53 +00:00
import * as plugins from './smartversion.plugins.js';
2017-08-17 14:15:47 +00:00
export class SmartVersion {
2022-04-26 12:44:53 +00:00
public static fromFuzzyString(fuzzyString: string): SmartVersion {
2021-04-26 07:16:26 +00:00
return new SmartVersion(plugins.semver.minVersion(fuzzyString).version, fuzzyString);
}
public originalVersionString: string;
public semver: plugins.semver.SemVer;
2022-04-26 12:44:53 +00:00
public get versionString(): string {
return this.semver.version;
};
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);
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
*/
2022-04-26 12:44:53 +00:00
public greaterThanString(versionStringArg: string) {
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
*/
2022-04-26 12:44:53 +00:00
public lessThanString(versionStringArg: string) {
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;
}
2022-04-26 12:44:53 +00:00
public getNewPatchVersion() {
const newInstance = new SmartVersion(`${this.semver.major}.${this.semver.minor}.${this.semver.patch + 1}`);
return newInstance;
}
public getNewMinorVersion() {
2022-04-28 10:20:09 +00:00
const newInstance = new SmartVersion(`${this.semver.major}.${this.semver.minor + 1}.${0}`);
2022-04-26 12:44:53 +00:00
return newInstance;
}
public getNewMajorVersion() {
2022-04-28 10:20:09 +00:00
const newInstance = new SmartVersion(`${this.semver.major + 1}.${0}.${0}`);
2022-04-26 12:44:53 +00:00
return newInstance;
}
2022-04-26 14:03:30 +00:00
public getNewVersion(typeArg: 'patch' | 'minor' | 'major') {
switch (typeArg) {
case 'patch':
return this.getNewPatchVersion();
case 'minor':
return this.getNewMinorVersion();
case 'major':
return this.getNewMajorVersion();
default:
throw new Error('unknown new version type.');
}
}
2017-08-17 14:15:47 +00:00
}