-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (45 loc) · 1.19 KB
/
index.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
var semver = require('semver-utils');
var fs = require('fs');
/**
* Bump semantic version (SemVer) number in VERSION according to MASKS. MASKS
* is an array composed of three digits, e.g. [0, 1, 0]. The topmost
* (left-most) nonzero bit of SemVer number is bumped (incremented) and the
* lower bits are set to zeros.
* @class SemverIncrement
*/
/**
* Call this function directly by requiring in Node.js runtime.
*
* @method main
* @param {Array} masks
* @param {String} version
* @return {String}
*/
module.exports = function (masks, version) {
"use strict";
if (typeof masks === 'string') {
version = masks;
masks = [0, 0, 0];
}
let bitMap = ['major', 'minor', 'patch'];
let bumpAt = 'patch';
let oldVer = version.match(/\d+/g);
for (let i = 0; i < masks.length; ++i) {
if (masks[i] === 1) {
bumpAt = bitMap[i];
break;
}
}
let bumpIdx = bitMap.indexOf(bumpAt);
let newVersion = []
for (let i = 0; i < oldVer.length; ++i) {
if (i < bumpIdx) {
newVersion[i] = +oldVer[i];
} else if (i === bumpIdx) {
newVersion[i] = +oldVer[i] + 1;
} else {
newVersion[i] = 0;
}
}
return newVersion.join('.');
};