-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
51 lines (43 loc) · 1.36 KB
/
update.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
const fs = require('fs');
const replace = require('replace-in-file');
const opt = process.argv[2];
// Modify version number
function updateVersion(version) {
fs.readFile('./package.json', 'utf-8', function(err, data) {
if (err) {
console.log(err);
}
let semver = JSON.parse(data).version;
const arr = semver.split('.');
if (version === 'major') {
arr[0] = parseInt(arr[0]) + 1;
arr[1] = '0';
arr[2] = '0';
} else if (version === 'minor') {
arr[1] = parseInt(arr[1]) + 1;
arr[2] = '0';
} else if (version === 'patch') {
arr[2] = parseInt(arr[2]) + 1;
} else if (!version) {
console.log('Skipping update version.');
return;
} else {
throw new Error(`${version} is invalid.`);
}
const newSemver = arr.join('.');
const options = {
files: './package.json',
from: /"version": "[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}"/,
to: `"version": "${newSemver}"`,
};
try {
console.log(`Updating to ${newSemver}`);
const results = replace.sync(options);
console.log(results)
}
catch (error) {
console.log(error);
}
})
}
updateVersion(opt);