-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-version.js
More file actions
executable file
·158 lines (131 loc) · 4.7 KB
/
update-version.js
File metadata and controls
executable file
·158 lines (131 loc) · 4.7 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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
/**
* Version update script for easyJSON project
* Updates version in package.json, tauri config files, and Cargo.toml
*/
// ANSI color codes for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function validateVersion(version) {
// Basic semantic versioning validation
const semverRegex = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/;
return semverRegex.test(version);
}
function updatePackageJson(version) {
const filePath = 'package.json';
try {
const content = fs.readFileSync(filePath, 'utf8');
const packageJson = JSON.parse(content);
const oldVersion = packageJson.version;
packageJson.version = version;
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2) + '\n');
log(`✓ Updated ${filePath}: ${oldVersion} → ${version}`, 'green');
return true;
} catch (error) {
log(`✗ Failed to update ${filePath}: ${error.message}`, 'red');
return false;
}
}
function updateTauriConfig(filePath, version) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const config = JSON.parse(content);
const oldVersion = config.version;
config.version = version;
fs.writeFileSync(filePath, JSON.stringify(config, null, 2) + '\n');
log(`✓ Updated ${filePath}: ${oldVersion} → ${version}`, 'green');
return true;
} catch (error) {
log(`✗ Failed to update ${filePath}: ${error.message}`, 'red');
return false;
}
}
function updateCargoToml(version) {
const filePath = 'src-tauri/Cargo.toml';
try {
let content = fs.readFileSync(filePath, 'utf8');
const oldVersionMatch = content.match(/^version = "([^"]+)"/m);
const oldVersion = oldVersionMatch ? oldVersionMatch[1] : 'unknown';
content = content.replace(/^version = "[^"]+"/m, `version = "${version}"`);
fs.writeFileSync(filePath, content);
log(`✓ Updated ${filePath}: ${oldVersion} → ${version}`, 'green');
return true;
} catch (error) {
log(`✗ Failed to update ${filePath}: ${error.message}`, 'red');
return false;
}
}
function getCurrentVersion() {
try {
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
return packageJson.version;
} catch (error) {
return 'unknown';
}
}
function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
log('Usage: node update-version.js <new-version>', 'yellow');
log('Example: node update-version.js 1.0.0', 'cyan');
log('Example: node update-version.js 1.0.0-beta.1', 'cyan');
process.exit(1);
}
const newVersion = args[0];
const currentVersion = getCurrentVersion();
log(`\n${colors.bright}Version Update Script${colors.reset}`);
log(`Current version: ${colors.cyan}${currentVersion}${colors.reset}`);
log(`New version: ${colors.cyan}${newVersion}${colors.reset}\n`);
if (!validateVersion(newVersion)) {
log('✗ Invalid version format. Please use semantic versioning (e.g., 1.0.0, 1.0.0-beta.1)', 'red');
process.exit(1);
}
if (newVersion === currentVersion) {
log('⚠ Version is already set to this value', 'yellow');
process.exit(0);
}
log('Updating files...\n');
const files = [
{ name: 'package.json', update: () => updatePackageJson(newVersion) },
{ name: 'src-tauri/tauri.conf.json', update: () => updateTauriConfig('src-tauri/tauri.conf.json', newVersion) },
{ name: 'src-tauri/tauri.conf.dev.json', update: () => updateTauriConfig('src-tauri/tauri.conf.dev.json', newVersion) },
{ name: 'src-tauri/Cargo.toml', update: () => updateCargoToml(newVersion) }
];
let successCount = 0;
for (const file of files) {
if (file.update()) {
successCount++;
}
}
log(`\n${colors.bright}Summary:${colors.reset}`);
log(`✓ Successfully updated ${successCount}/${files.length} files`, successCount === files.length ? 'green' : 'yellow');
if (successCount === files.length) {
log('\n🎉 All files updated successfully!', 'green');
log('You can now commit your changes:', 'cyan');
log(' git add .', 'cyan');
log(` git commit -m "chore: bump version to ${newVersion}"`, 'cyan');
} else {
log('\n⚠ Some files failed to update. Please check the errors above.', 'yellow');
process.exit(1);
}
}
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
log(`\n✗ Unexpected error: ${error.message}`, 'red');
process.exit(1);
});
// Run the script
main();