|
| 1 | +// scripts/sync-package-versions.js |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { execSync } from 'child_process'; |
| 5 | + |
| 6 | +const packagesDir = path.resolve('packages'); |
| 7 | + |
| 8 | +const getLatestTag = (prefix) => { |
| 9 | + try { |
| 10 | + const tag = execSync( |
| 11 | + `git tag --list '${prefix}@*' --sort=-v:refname` |
| 12 | + ) |
| 13 | + .toString() |
| 14 | + .split('\n')[0] |
| 15 | + .trim(); |
| 16 | + return tag.split('@')[1]; // returns the version |
| 17 | + } catch (e) { |
| 18 | + console.error(`❌ Failed to get tag for ${prefix}`); |
| 19 | + return null; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +const updatePackageVersion = (pkg, version) => { |
| 24 | + const pkgPath = path.join(packagesDir, pkg, 'package.json'); |
| 25 | + const pkgJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 26 | + if (pkgJson.version !== version) { |
| 27 | + pkgJson.version = version; |
| 28 | + fs.writeFileSync( |
| 29 | + pkgPath, |
| 30 | + JSON.stringify(pkgJson, null, 2) + '\n' |
| 31 | + ); |
| 32 | + console.log(`✅ Updated ${pkg} to version ${version}`); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +const packages = fs |
| 37 | + .readdirSync(packagesDir) |
| 38 | + .filter((dir) => |
| 39 | + fs.existsSync(path.join(packagesDir, dir, 'package.json')) |
| 40 | + ); |
| 41 | + |
| 42 | +let updatedPackages = []; |
| 43 | + |
| 44 | +// Update versions |
| 45 | +for (const pkg of packages) { |
| 46 | + const version = getLatestTag(pkg); |
| 47 | + if (version) { |
| 48 | + updatePackageVersion(pkg, version); |
| 49 | + updatedPackages.push({ pkg, version }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +try { |
| 54 | + const status = execSync('git status --porcelain').toString().trim(); |
| 55 | + |
| 56 | + if (status) { |
| 57 | + execSync('git config user.name "chimp-bot"'); |
| 58 | + execSync('git config user.email "chimp-bot@chimp-stack.com"'); |
| 59 | + execSync('git add packages/**/package.json'); |
| 60 | + const versions = updatedPackages |
| 61 | + .map((p) => `${p.pkg}@${p.version}`) |
| 62 | + .join(', '); |
| 63 | + const commitMessage = `chore(release): 🚀 sync versions – ${versions} [skip ci]`; |
| 64 | + |
| 65 | + execSync(`git commit -m "${commitMessage}"`); |
| 66 | + execSync('git push'); |
| 67 | + console.log('🚀 Pushed synced versions'); |
| 68 | + } else { |
| 69 | + console.log( |
| 70 | + '🤫 Nothing to commit — working tree clean. All synced.' |
| 71 | + ); |
| 72 | + } |
| 73 | +} catch (err) { |
| 74 | + console.error( |
| 75 | + '🧨 Version sync failed, but silence is golden in CI:', |
| 76 | + err.message |
| 77 | + ); |
| 78 | + process.exit(0); // Don't break the build if nothing needs syncing |
| 79 | +} |
0 commit comments