-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateMe.js
152 lines (122 loc) · 4.66 KB
/
updateMe.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
This should be a tiny npm package :)
updateMe
- will update any json file that have a version property .. or not :D
2 ways use masking - can increment arbitr
node updateMe.js ./manifestmv3.json --x x.x.+3
simple verbose - only increment by one
node updateMe.js ./manifestmv3.json --bump major
node updateMe.js ./manifestmv3.json --bump minor
node updateMe.js ./manifestmv3.json --bump patch
lazy way - increment by one with a rolling increment - not hooked
node updateMe.js ./manifestmv3.json
*/
const fs = require('fs')
function updateVersion(fileName, default_inc = 1, minor_rollover = 10, patch_rollover = 10) {
// Read the manifest file
const manifest = require(fileName);
let version = manifest.version;
if (!version){
version = '0.0.0'
}
let [major, minor, patch] = version.split('.').map(Number);
patch += default_inc;
if (patch > patch_rollover) {
patch = 0;
minor += 1;
if (minor > minor_rollover) {
minor = 0;
major += 1
}
}
const newVersion = `${major}.${minor}.${patch}`;
console.log(`Updating ${fileName}: ${manifest.version} => ${newVersion}`)
manifest.version = newVersion;
// Stringify the updated manifest with pretty-printing
const manifest_JSON = JSON.stringify(manifest, null, 2);
// Write the updated manifest back to the file
fs.writeFileSync(fileName, manifest_JSON, { encoding: 'utf8', flag: 'w' });
return manifest_JSON;
}
function updateVersionWithMask(fileName, mask){
const manifest = require(fileName);
let version = manifest.version;
if (!version){
version = '0.0.0'
}
const newVersion = maskVersion(version, mask)
console.log(`Updating ${fileName}: ${manifest.version} => ${newVersion}`)
if (Number.isNaN(newVersion)){
throw new Error (`Could NOT update version of ${fileName} to ${newVersion}`)
}
manifest.version = newVersion;
const manifest_JSON = JSON.stringify(manifest, null, 2);
fs.writeFileSync(fileName, manifest_JSON, { encoding: 'utf8', flag: 'w' });
return manifest_JSON;
}
function maskVersion(version, mask) {
const versionArray = version.split('.').map(Number)
console.log(mask)
const maskArray = mask.split('.').map(part => {
if (part === 'x' | 'X' ) return null;
if (part.startsWith('+')) return '+' + (parseInt(part.substring(1)) || 0);
return parseInt(part);
});
const maskedVersion = maskArray.map((maskPart, index) => {
const versionPart = versionArray[index];
if (maskPart === null) {
return versionPart !== null ? versionPart.toString() : 0;
} else if (maskPart.toString().startsWith('+')) {
if (versionPart === null) {
return maskPart;
} else {
return versionPart + parseInt(maskPart);
}
} else {
return maskPart.toString();
}
});
return maskedVersion.join('.');
}
try {
const args = process.argv.slice(2);
let default_inc = 1;
let minor_rollover = 10;
let patch_rollover = 10;
let mask = null
let [filename, subcmd, subcmd_arg, ...rest] = args;
// console.log({filename, subcmd, subcmd_arg, rest})
if (!filename) {
throw new Error('Please provide a filename. \n\tEx: updateMe.js [filename] --x [versionMask]');
}
if (subcmd === '--x') {
console.log("Usage: node updateMe.js [filename] --x [versionMask]");
if (!subcmd_arg || (subcmd_arg.length < 5)){
throw new Error("Invalid versionMask, expects x.x.x syntax")
}
return updateVersionWithMask(filename, subcmd_arg) // js... returning outside a fn... and it's fine???
} else if (subcmd === '--bump') {
if (!["major", "minor", "patch"].includes(subcmd_arg)){
console.log("Usage: node updateMe.js [filename] --bump <major|minor|patch>")
throw new Error("Invalid bump argument...")
}
switch (subcmd_arg) {
case 'major': mask = "+1.x.x"; break;
case 'minor': mask = "x.+1.x"; break;
case 'patch': mask = "x.x.+1"; break;
}
return updateVersionWithMask(filename, mask)
} else {
throw new Error("Please use a valid option: \n\tnode updateMe.js [filename] --x [versionMask] \n\tnode updateMe.js [filename] --bump <major|minor|patch>");
}
} catch (error) {
console.error(`Error: ${error.message}`, error);
process.exit(1);
}
module.exports = {
updateVersion, //updateVersion("./manifest.json")
updateVersionWithMask,
maskVersion
}
// TODO: sync multiple file to the same version
// sync and bump all files.