-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathversionUpdater.js
141 lines (121 loc) · 3.6 KB
/
versionUpdater.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const _ = require('underscore');
const SEMANTIC_VERSION_LEVELS = {
MAJOR: 'MAJOR',
MINOR: 'MINOR',
PATCH: 'PATCH',
BUILD: 'BUILD',
};
const MAX_INCREMENTS = 99;
/**
* Transforms a versions string into a number
*
* @param {String} versionString
* @returns {Array}
*/
const getVersionNumberFromString = (versionString) => {
const [version, build] = versionString.split('-');
const [major, minor, patch] = _.map(version.split('.'), (n) => Number(n));
return [major, minor, patch, Number.isInteger(Number(build)) ? Number(build) : 0];
};
/**
* Transforms version numbers components into a version string
*
* @param {Number} major
* @param {Number} minor
* @param {Number} patch
* @param {Number} [build]
* @returns {String}
*/
const getVersionStringFromNumber = (major, minor, patch, build = 0) => `${major}.${minor}.${patch}-${build}`;
/**
* Increments a minor version
*
* @param {Number} major
* @param {Number} minor
* @returns {String}
*/
const incrementMinor = (major, minor) => {
if (minor < MAX_INCREMENTS) {
return getVersionStringFromNumber(major, minor + 1, 0, 0);
}
return getVersionStringFromNumber(major + 1, 0, 0, 0);
};
/**
* Increments a Patch version
*
* @param {Number} major
* @param {Number} minor
* @param {Number} patch
* @returns {String}
*/
const incrementPatch = (major, minor, patch) => {
if (patch < MAX_INCREMENTS) {
return getVersionStringFromNumber(major, minor, patch + 1, 0);
}
return incrementMinor(major, minor);
};
/**
* Increments a build version
*
* @param {String} version
* @param {String} level
* @returns {String}
*/
const incrementVersion = (version, level) => {
const [major, minor, patch, build] = getVersionNumberFromString(version);
// Majors will always be incremented
if (level === SEMANTIC_VERSION_LEVELS.MAJOR) {
return getVersionStringFromNumber(major + 1, 0, 0, 0);
}
if (level === SEMANTIC_VERSION_LEVELS.MINOR) {
return incrementMinor(major, minor);
}
if (level === SEMANTIC_VERSION_LEVELS.PATCH) {
return incrementPatch(major, minor, patch);
}
if (build < MAX_INCREMENTS) {
return getVersionStringFromNumber(major, minor, patch, build + 1);
}
return incrementPatch(major, minor, patch);
};
/**
* @param {String} currentVersion
* @param {String} level
* @returns {String}
*/
function getPreviousVersion(currentVersion, level) {
const [major, minor, patch, build] = getVersionNumberFromString(currentVersion);
if (level === SEMANTIC_VERSION_LEVELS.MAJOR) {
if (major === 1) {
return getVersionStringFromNumber(1, 0, 0, 0);
}
return getVersionStringFromNumber(major - 1, 0, 0, 0);
}
if (level === SEMANTIC_VERSION_LEVELS.MINOR) {
if (minor === 0) {
return getPreviousVersion(currentVersion, SEMANTIC_VERSION_LEVELS.MAJOR);
}
return getVersionStringFromNumber(major, minor - 1, 0, 0);
}
if (level === SEMANTIC_VERSION_LEVELS.PATCH) {
if (patch === 0) {
return getPreviousVersion(currentVersion, SEMANTIC_VERSION_LEVELS.MINOR);
}
return getVersionStringFromNumber(major, minor, patch - 1, 0);
}
if (build === 0) {
return getPreviousVersion(currentVersion, SEMANTIC_VERSION_LEVELS.PATCH);
}
return getVersionStringFromNumber(major, minor, patch, build - 1);
}
export {
getVersionNumberFromString,
getVersionStringFromNumber,
incrementVersion,
// For tests
MAX_INCREMENTS,
SEMANTIC_VERSION_LEVELS,
incrementMinor,
incrementPatch,
getPreviousVersion,
};