This repository was archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-version.js
More file actions
261 lines (244 loc) · 7.3 KB
/
update-version.js
File metadata and controls
261 lines (244 loc) · 7.3 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env node
const child = require('child_process');
const { randomBytes } = require('crypto');
const fs = require('fs');
const path = require('path');
const process = require('process');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
const _package = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), { encoding: 'utf-8' }));
const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, 'src', 'manifest'), { encoding: 'utf-8' }));
const __data = {
major: Number(0),
minor: Number(0),
patch: Number(0),
version: String(''),
force: Boolean(false),
bypass: Boolean(false),
};
const commands = [
{
commands: ['-h', '--help'],
nArgs: 0,
func: () =>
{
console.log('Options:');
console.log('--help -h : Print help');
console.log('--force -f : Force using defined version and not calculated version');
console.log('--bypass -b : Bypass the end validation question');
console.log('--major -ma : Set major number, beetween `0` to `99` included');
console.log('--minor -mi : Set minor number, beetween `0` to `99` included');
console.log('--patch -p : Set patch number, beetween `0` to `99` included');
console.log('--version -v : Set version, `alpha` or `beta`. If you want to delete version, set `delete`');
process.exit(0);
},
},
{
commands: ['-b', '--bypass'],
nArgs: 0,
func: () =>
{
__data.bypass = Boolean(true);
},
},
{
commands: ['-f', '--force'],
nArgs: 0,
func: () =>
{
__data.force = Boolean(true);
},
},
{
commands: ['-ma', '--major'],
nArgs: 1,
func: (args) =>
{
const regex = /^([0-9]{1,2})$/;
if (!regex.test(args[0]))
throw new Error(`version \x1b[96m${args[0]}\x1b[0m is incorrect, please use (0-99)`);
__data.major = Number(args[0]);
},
},
{
commands: ['-mi', '--minor'],
nArgs: 1,
func: (args) =>
{
const regex = /^([0-9]{1,2})$/;
if (!regex.test(args[0]))
throw new Error(`version \x1b[96m${args[0]}\x1b[0m is incorrect, please use (0-99)`);
__data.minor = Number(args[0]);
},
},
{
commands: ['-p', '--patch'],
nArgs: 1,
func: (args) =>
{
const regex = /^([0-9]{1,2})$/;
if (!regex.test(args[0]))
throw new Error(`version \x1b[96m${args[0]}\x1b[0m is incorrect, please use (0-99)`);
__data.patch = Number(args[0]);
},
},
{
commands: ['-v', '--version'],
nArgs: 1,
func: (args) =>
{
const regex = /^(alpha|beta|delete)$/;
if (!regex.test(args[0]))
throw new Error(`version \x1b[96m${args[0]}\x1b[0m is incorrect, please use 'alpha' or 'beta'. If you want to delete version, set 'delete'`);
__data.version = String(args[0]);
},
},
];
const printError = (message = '') => console.error(`\x1b[31m\x1b[4mError\x1b[0m\x1b[31m:\x1b[0m ${message}`);
const calcVersion = (version) =>
{
const calc = (current, add, isHundred = false) =>
{
let cur = Number(current) + Number(add);
let rest = Number(0);
while (cur > 99)
{
if (isHundred && cur === 100)
return { cur: 99, rest };
cur -= 100;
++rest;
}
return { cur, rest };
};
const newVersion = {
unit: Number(0),
ten: Number(0),
hundred: Number(0),
version: String(''),
};
const oldVersion = /^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})(?:-(alpha|beta))?$/.exec(version);
const unit = calc(Number(oldVersion[3]), __data.patch);
newVersion.unit = unit.cur;
const ten = calc(Number(oldVersion[2]) + unit.rest, __data.minor);
newVersion.ten = ten.cur;
const hundred = calc(Number(oldVersion[1]) + ten.rest, __data.major, true);
newVersion.hundred = hundred.cur;
newVersion.version = String(__data.version);
let ret = `${newVersion.hundred}.${newVersion.ten}.${newVersion.unit}`;
if (newVersion.version.length && newVersion.version !== 'delete')
ret += `-${newVersion.version}`;
else if (newVersion.version !== 'delete')
ret += `-${oldVersion[4]}`;
return (ret);
};
const endQuestion = (oldValue, newValue) =>
{
const val = () =>
{
_package.version = newValue;
manifest.version.software = newValue;
try
{
fs.writeFileSync(path.join(__dirname, 'package.json'), JSON.stringify(_package, null, 2), { encoding: 'utf-8', flag: 'w' });
fs.writeFileSync(path.join(__dirname, 'src', 'manifest'), JSON.stringify(manifest, null, 2), { encoding: 'utf-8', flag: 'w' });
}
catch (err)
{
throw new Error(err.message);
}
console.log('Version modification have been made');
};
if (__data.bypass)
{
val();
process.exit(0);
}
readline.question(`The version will change from \x1b[96m${oldValue}\x1b[0m to \x1b[92m${newValue}\x1b[0m, is this correct ? (\x1b[92myes\x1b[0m | \x1b[91mno\x1b[0m) `, (e) =>
{
if (e !== 'yes' && e !== 'no')
{
printError('response is not yes or no, abort');
process.exit(1);
}
if (e === 'no')
{
printError('abort modification');
process.exit(1);
}
val();
readline.question('Would you like to create a new release automatically and publish it immediately on GitHub ? (\x1b[92myes\x1b[0m | \x1b[91mno\x1b[0m) ', (e2) =>
{
if (e2 !== 'yes' && e2 !== 'no')
{
printError('response is not yes or no, abort');
process.exit(1);
}
if (e2 === 'no')
{
printError('abort modification');
process.exit(1);
}
const _child = child.spawn(`git add * && git commit -m "automatic commit [${newValue}] - ${randomBytes(12).toString('hex').slice(0, 12)}" && git tag ${newValue} && git push && git push --tags`, { shell: true });
_child.stdout.on('data', (data) => console.log(data.toString()));
_child.stderr.on('data', (data) => console.error(data.toString()));
_child.on('close', () =>
{
console.log(`The release has been put online. Go to ${_package.repository.url} to check that the ci/cd is working well`);
readline.close();
});
});
});
};
const getCommand = (options) =>
{
for (const el of commands)
if (el.commands.indexOf(options) !== -1)
return el;
return undefined;
};
const main = () =>
{
console.log('┌──────────────────────────────────────────┐');
console.log(' \x1b[36mupdate-version\x1b[0m');
console.log(' Easily update version of \x1b[94mMapcraft\x1b[0m');
console.log(' copyright © Clément Bertrand,2022');
console.log('└──────────────────────────────────────────┘');
let newVersion;
if (process.argv.length < 3)
{
printError('no argument is passed');
process.exit(1);
}
for (let x = 2; process.argv[x]; x++)
{
const args = [];
const command = getCommand(process.argv[x]);
try
{
if (command === undefined)
throw new Error(`option ${process.argv[x]} is undefined`);
if (command.nArgs > 0)
{
const limit = x + command.nArgs;
if (limit >= process.argv.length)
throw new Error(`option ${process.argv[x]} need ${command.nArgs} argument${(command.nArgs > 1) ? 's' : ''}`);
for (++x; x <= limit; x++)
args.push(process.argv[x]);
--x;
}
command.func(args);
}
catch (err)
{
printError(err.message);
process.exit(1);
}
}
if (!__data.force)
newVersion = calcVersion(_package.version);
else
newVersion = `${__data.major}.${__data.minor}.${__data.patch}${(__data.version.length) ? `-${__data.version}` : ''}`;
endQuestion(_package.version, newVersion);
}; main();