|
| 1 | +#!/usr/bin/env tsx |
| 2 | +/** |
| 3 | + * Sync versions across workspace packages |
| 4 | + * |
| 5 | + * This script ensures all workspace packages use the same version as the root package.json. |
| 6 | + * It reads the version from the root package.json and updates all packages in the monorepo. |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * pnpm sync-versions [--dry-run] |
| 10 | + */ |
| 11 | + |
| 12 | +import fs from 'node:fs/promises'; |
| 13 | +import path from 'node:path'; |
| 14 | +import { fileURLToPath } from 'node:url'; |
| 15 | + |
| 16 | +const __filename = fileURLToPath(import.meta.url); |
| 17 | +const __dirname = path.dirname(__filename); |
| 18 | + |
| 19 | +const ROOT_DIR = path.resolve(__dirname, '..'); |
| 20 | +const PACKAGES_DIR = path.join(ROOT_DIR, 'packages'); |
| 21 | + |
| 22 | +interface PackageJson { |
| 23 | + name: string; |
| 24 | + version: string; |
| 25 | + [key: string]: any; |
| 26 | +} |
| 27 | + |
| 28 | +async function readJsonFile(filePath: string): Promise<PackageJson> { |
| 29 | + const content = await fs.readFile(filePath, 'utf-8'); |
| 30 | + return JSON.parse(content); |
| 31 | +} |
| 32 | + |
| 33 | +async function writeJsonFile(filePath: string, data: PackageJson): Promise<void> { |
| 34 | + const content = JSON.stringify(data, null, 2) + '\n'; |
| 35 | + await fs.writeFile(filePath, content, 'utf-8'); |
| 36 | +} |
| 37 | + |
| 38 | +async function getPackageDirs(): Promise<string[]> { |
| 39 | + const entries = await fs.readdir(PACKAGES_DIR, { withFileTypes: true }); |
| 40 | + return entries |
| 41 | + .filter(entry => entry.isDirectory()) |
| 42 | + .map(entry => path.join(PACKAGES_DIR, entry.name)); |
| 43 | +} |
| 44 | + |
| 45 | +async function syncVersions(dryRun: boolean = false): Promise<void> { |
| 46 | + console.log('🔄 Syncing workspace package versions...\n'); |
| 47 | + |
| 48 | + // Read root package.json version |
| 49 | + const rootPackageJsonPath = path.join(ROOT_DIR, 'package.json'); |
| 50 | + const rootPackage = await readJsonFile(rootPackageJsonPath); |
| 51 | + const targetVersion = rootPackage.version; |
| 52 | + |
| 53 | + console.log(`📦 Root version: ${targetVersion}\n`); |
| 54 | + |
| 55 | + // Get all package directories |
| 56 | + const packageDirs = await getPackageDirs(); |
| 57 | + |
| 58 | + let updated = 0; |
| 59 | + let skipped = 0; |
| 60 | + let errors = 0; |
| 61 | + |
| 62 | + for (const packageDir of packageDirs) { |
| 63 | + const packageJsonPath = path.join(packageDir, 'package.json'); |
| 64 | + |
| 65 | + try { |
| 66 | + const pkg = await readJsonFile(packageJsonPath); |
| 67 | + const packageName = pkg.name; |
| 68 | + const currentVersion = pkg.version; |
| 69 | + |
| 70 | + if (currentVersion === targetVersion) { |
| 71 | + console.log(`✓ ${packageName}: ${currentVersion} (already synced)`); |
| 72 | + skipped++; |
| 73 | + } else { |
| 74 | + console.log(`⚠ ${packageName}: ${currentVersion} → ${targetVersion}`); |
| 75 | + |
| 76 | + if (!dryRun) { |
| 77 | + pkg.version = targetVersion; |
| 78 | + await writeJsonFile(packageJsonPath, pkg); |
| 79 | + console.log(` ✓ Updated`); |
| 80 | + } else { |
| 81 | + console.log(` ℹ Would update (dry run)`); |
| 82 | + } |
| 83 | + updated++; |
| 84 | + } |
| 85 | + } catch (error) { |
| 86 | + console.error(`✗ Error processing ${path.basename(packageDir)}:`, error); |
| 87 | + errors++; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + console.log(`\n${'='.repeat(50)}`); |
| 92 | + console.log(`Summary:`); |
| 93 | + console.log(` Updated: ${updated}`); |
| 94 | + console.log(` Already synced: ${skipped}`); |
| 95 | + console.log(` Errors: ${errors}`); |
| 96 | + |
| 97 | + if (dryRun && updated > 0) { |
| 98 | + console.log(`\n💡 Run without --dry-run to apply changes`); |
| 99 | + } else if (!dryRun && updated > 0) { |
| 100 | + console.log(`\n✅ Version sync complete!`); |
| 101 | + } else if (updated === 0 && errors === 0) { |
| 102 | + console.log(`\n✅ All packages already in sync!`); |
| 103 | + } |
| 104 | + |
| 105 | + if (errors > 0) { |
| 106 | + process.exit(1); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Parse CLI args |
| 111 | +const args = process.argv.slice(2); |
| 112 | +const dryRun = args.includes('--dry-run'); |
| 113 | + |
| 114 | +syncVersions(dryRun).catch(error => { |
| 115 | + console.error('Fatal error:', error); |
| 116 | + process.exit(1); |
| 117 | +}); |
0 commit comments