-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-version.js
69 lines (59 loc) · 1.65 KB
/
gulp-version.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
const path = require('path'),
fs = require('fs'),
readline = require('readline'),
yargs = require('yargs');
const VERSION_TYPES = {
MAJOR: 'major',
MINOR: 'minor',
PATCH: 'patch',
BUILD: 'build'
};
function getCurrentVersion(text) {
let index = text.match(/version="*"/i).index;
return text.substr(index).split('"')[1];
}
function getVersionType(argv) {
if (!argv) argv = yargs.argv;
if (argv.major) return VERSION_TYPES.MAJOR;
if (argv.minor) return VERSION_TYPES.MINOR;
return VERSION_TYPES.PATCH;
}
function getNewVersion(line) {
let digits = getCurrentVersion(line).split('.').map(digit => Number(digit));
switch (getVersionType()) {
case VERSION_TYPES.MAJOR:
digits[0] += 1;
digits[1] = digits[2] = 0;
break;
case VERSION_TYPES.MINOR:
digits[1] += 1;
digits[2] = 0;
break;
default:
digits[2] += 1;
}
return digits.join('.');
}
function execConfig() {
let currentVersion, newVersion, content = '';
let filePath = path.resolve(__dirname, 'config.xml');
let input = fs.createReadStream(filePath);
let lineReader = readline.createInterface({ input });
lineReader
.on('line', line => {
if ((line.indexOf('widget') > -1) && (line.indexOf('version') > -1)) {
currentVersion = getCurrentVersion(line);
newVersion = getNewVersion(line);
line = line.replace(/version="([0-9|\.]+)?"/i, `version="${newVersion}"`);
}
content += line + '\n';
})
.on('close', () => {
fs.writeFileSync(filePath, content, { encoding: 'utf8' });
console.log(`\t > Updated app version: "${currentVersion}" -> "${newVersion}" (${getVersionType()})`);
});
}
module.exports = {
getType: getVersionType,
exec: execConfig,
};