generated from MisaLiu/LiteLoaderQQNT-PluginTemplate-Vite
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bump.cjs
64 lines (54 loc) · 1.75 KB
/
bump.cjs
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
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');
function incrementVersion(version, type) {
const [major, minor, patch] = version.split('.').map(Number);
switch (type) {
case 'major':
return `${major + 1}.0.0`;
case 'minor':
return `${major}.${minor + 1}.0`;
case 'patch':
return `${major}.${minor}.${patch + 1}`;
default:
throw new Error('Invalid version type');
}
}
function updateVersionInFile(filePath, type) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file: ${filePath}`);
return;
}
let json;
try {
json = JSON.parse(data);
} catch (parseError) {
console.error(`Error parsing JSON in file: ${filePath}`);
return;
}
if (!json.version) {
console.error(`No version found in ${filePath}`);
return;
}
const incrementedVersion = incrementVersion(json.version, type);
const before = json.version;
json.version = incrementedVersion;
fs.writeFile(filePath, JSON.stringify(json, null, 2), 'utf8', err => {
if (err) {
console.error(`Error writing to file: ${filePath}`);
return;
}
console.log(`Version updated successfully in ${filePath} from ${before} to ${incrementedVersion}`);
});
});
}
// Parse command line arguments
const args = process.argv.slice(2);
const versionType = args[0];
if (!versionType || !['--major', '--minor', '--patch'].includes(versionType)) {
console.error('Usage: node increment_version.js <--major|--minor|--patch>');
process.exit(1);
}
// Update versions in both files
updateVersionInFile('manifest.json', versionType.slice(2));
updateVersionInFile('package.json', versionType.slice(2));