-
Notifications
You must be signed in to change notification settings - Fork 13
/
versionNumber.ts
126 lines (114 loc) · 3.85 KB
/
versionNumber.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Messages } from '@salesforce/core';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/packaging', 'version_number');
export enum BuildNumberToken {
LATEST_BUILD_NUMBER_TOKEN = 'LATEST',
NEXT_BUILD_NUMBER_TOKEN = 'NEXT',
RELEASED_BUILD_NUMBER_TOKEN = 'RELEASED',
HIGHEST_VERSION_NUMBER_TOKEN = 'HIGHEST',
NONE_VERSION_NUMBER_TOKEN = 'NONE',
}
export class VersionNumber {
public constructor(
public major: string | number,
public minor: string | number,
public patch: string | number,
public build: string | number
) {}
/**
* Separates at major.minor string into {major: Number, minor: Number} object
*
* @param versionString a string in the format of major.minor like '3.2'
*/
public static parseMajorMinor(versionString: string): { major: number | null; minor: number | null } {
const versions = versionString?.split('.');
if (!versions) {
// return nulls so when no version option is provided, the server can infer the correct version
return { major: null, minor: null };
}
if (versions.length === 2) {
return {
major: Number(versions[0]),
minor: Number(versions[1]),
};
} else {
throw messages.createError('invalidMajorMinorFormat', [versionString]);
}
}
public static from(versionString: string | undefined): VersionNumber {
if (!versionString) {
throw messages.createError('errorMissingVersionNumber');
}
const version = versionString.split('.');
if (version?.length === 4) {
const [major, minor, patch, build] = version;
const asNumbers = [major, minor, patch, build].map((v) => parseInt(v, 10));
if (asNumbers.slice(0, 3).some((v) => isNaN(v))) {
throw messages.createError('errorInvalidMajorMinorPatchNumber', [versionString]);
}
if (isNaN(asNumbers[3]) && !VersionNumber.isABuildKeyword(build)) {
throw messages.createError('errorInvalidBuildNumberToken', [
versionString,
Object.values(BuildNumberToken).join(', '),
]);
}
return new VersionNumber(major, minor, patch, build);
}
throw messages.createError('errorInvalidVersionNumber', [versionString]);
}
public static isABuildKeyword(token: string | number): boolean {
const buildNumberTokenValues = Object.values(BuildNumberToken);
const results = buildNumberTokenValues.includes(token as BuildNumberToken);
return results;
}
public toString(): string {
{
return `${this.major || '0'}.${this.minor || '0'}.${this.patch || '0'}.${this.build ? `${this.build}` : '0'}`;
}
}
/**
* @deprecated use isBuildKeyword instead
*/
public isbuildKeyword(): boolean {
return this.isBuildKeyword();
}
public isBuildKeyword(): boolean {
return VersionNumber.isABuildKeyword(this.build);
}
public compareTo(other: VersionNumber): number {
const [aMajor, aMinor, aPatch, aBuild] = [this.major, this.minor, this.patch, this.build].map((v) =>
typeof v === 'number' ? v : parseInt(v, 10)
);
const [oMajor, oMinor, oPatch, oBuild] = [other.major, other.minor, other.patch, other.build].map((v) =>
typeof v === 'number' ? v : parseInt(v, 10)
);
if (aMajor !== oMajor) {
return aMajor - oMajor;
}
if (aMinor !== oMinor) {
return aMinor - oMinor;
}
if (aPatch !== oPatch) {
return aPatch - oPatch;
}
if (isNaN(aBuild) && isNaN(oBuild)) {
return 0;
}
if (isNaN(aBuild)) {
return 1;
}
if (isNaN(oBuild)) {
return -1;
}
if (aBuild !== oBuild) {
return aBuild - oBuild;
}
return 0;
}
}