-
Notifications
You must be signed in to change notification settings - Fork 6
/
version-bump.js
35 lines (29 loc) · 872 Bytes
/
version-bump.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
const fs = require("fs");
// get information which type of version bump should be applied
const type = process.argv[2];
const types = ["major", "minor", "patch"];
let toUpdate = types.findIndex(t => type === t);
if (toUpdate < 0) {
toUpdate = 2;
}
const packagePath = "package.json";
// get current version
const packageFileString = fs.readFileSync(packagePath).toString();
const packageJson = JSON.parse(packageFileString);
const { version } = packageJson;
// gengerate new version
const versionArray = version.split(".");
const newVersion = versionArray
.map((v, i) => {
if (i < toUpdate) {
return v;
}
if (i === toUpdate) {
return parseInt(v, 10) + 1;
}
return 0;
})
.join(".");
// update version in each package
packageJson.version = newVersion;
fs.writeFileSync(packagePath, `${JSON.stringify(packageJson, null, 3)}\n`);