forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version-parser.js
59 lines (55 loc) · 1.69 KB
/
version-parser.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
'use strict';
const semver = require('semver');
const VALID_VERSION_REGEX = /^v\d+\.\d+\.\d+(?:-([a-zA-Z][0-9a-zA-Z-]*))?/;
const STABLE_VERSION_REGEX = /^v\d+\.\d+\.\d+$/;
module.exports = {
isValidVersion: (version) => {
return VALID_VERSION_REGEX.test(version);
},
isValidStableVersion: (version) => {
return STABLE_VERSION_REGEX.test(version);
},
incrementPatch: (version) => {
const newVersion = 'v' + semver.inc(version, 'patch');
if (!newVersion) {
throw new Error(`Error incrementing patch for version "${version}"`);
}
return newVersion;
},
isGreaterOrEqual: (newVersion, previousVersion) => {
return semver.gte(newVersion, previousVersion);
},
// returns true if the provided version is definitely greater than any existing
// auto generated alpha versions
isDefinitelyGreaterThanAlphas: (version) => {
const parsed = semver.parse(version, { loose: false, includePrerelease: true });
if (!parsed) {
throw new Error('Error parsing version.');
}
// anything after a part of `0` must be greater than `alpha`
let hadZero = false;
return parsed.prerelease.every((part) => {
if (hadZero && part <= 'alpha') {
return false;
} else {
hadZero = false;
}
if (part === 0) {
hadZero = true;
}
return true;
});
},
// extract what we should use as the npm dist-tag (https://docs.npmjs.com/cli/dist-tag)
// e.g
// v1.2.3-beta => beta
// v1.2.3-beta.1 => beta
// v1.2.3 => latest
getVersionTag: (version) => {
const match = VALID_VERSION_REGEX.exec(version);
if (!match) {
throw new Error('Invalid version.');
}
return match[1] || 'latest';
}
};