const fs = require('fs');
const path = require('path');
const args = process.argv.slice(2);
if (args.length === 0) {
console.error('Usage: node sync-version.js <version> or node sync-version.js patch|minor|major');
process.exit(1);
}
const files = [
{ path: 'Cargo.toml', type: 'toml' },
{ path: 'napi/package.json', type: 'json' },
{ path: 'napi/npm/darwin-arm64/package.json', type: 'json' },
{ path: 'napi/npm/darwin-x64/package.json', type: 'json' },
{ path: 'napi/npm/linux-arm64-gnu/package.json', type: 'json' },
{ path: 'napi/npm/linux-x64-gnu/package.json', type: 'json' },
{ path: 'napi/npm/win32-x64-msvc/package.json', type: 'json' },
];
function findProjectRoot() {
let dir = __dirname;
while (dir !== path.dirname(dir)) {
if (fs.existsSync(path.join(dir, 'Cargo.toml')) &&
fs.existsSync(path.join(dir, 'napi'))) {
return dir;
}
dir = path.dirname(dir);
}
throw new Error('Could not find project root');
}
const projectRoot = findProjectRoot();
function getCurrentVersion() {
const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, 'napi/package.json'), 'utf8'));
return packageJson.version;
}
function bumpVersion(currentVersion, bumpType) {
const parts = currentVersion.split('.').map(Number);
switch (bumpType) {
case 'patch':
parts[2]++;
break;
case 'minor':
parts[1]++;
parts[2] = 0;
break;
case 'major':
parts[0]++;
parts[1] = 0;
parts[2] = 0;
break;
default:
return bumpType;
}
return parts.join('.');
}
function updateVersion(filePath, fileType, newVersion) {
const fullPath = path.join(projectRoot, filePath);
if (!fs.existsSync(fullPath)) {
console.warn(`Warning: ${filePath} does not exist, skipping...`);
return;
}
if (fileType === 'json') {
const content = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
content.version = newVersion;
fs.writeFileSync(fullPath, JSON.stringify(content, null, 2) + '\n');
} else if (fileType === 'toml') {
let content = fs.readFileSync(fullPath, 'utf8');
content = content.replace(
/^version = ".*"$/m,
`version = "${newVersion}"`
);
fs.writeFileSync(fullPath, content);
}
}
try {
const currentVersion = getCurrentVersion();
console.log(`Current version: ${currentVersion}`);
const newVersion = bumpVersion(currentVersion, args[0]);
console.log(`New version: ${newVersion}`);
if (!/^\d+\.\d+\.\d+$/.test(newVersion)) {
console.error('Invalid version format. Expected format: X.Y.Z');
process.exit(1);
}
console.log('\nUpdating versions...');
for (const file of files) {
console.log(` ✓ ${file.path}`);
updateVersion(file.path, file.type, newVersion);
}
console.log('\n✅ All versions synchronized to', newVersion);
console.log('\nNext steps:');
console.log('1. Review the changes: git diff');
console.log('2. Commit the changes: git add -A && git commit -m "v' + newVersion + '"');
console.log('3. Create a tag: git tag v' + newVersion);
console.log('4. Push changes and tag: git push && git push --tags');
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}