|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Post-merge hook to preserve alpha versions when merging master into alpha branch |
| 5 | + * |
| 6 | + * This script runs after a merge and checks if: |
| 7 | + * 1. We're on the alpha branch |
| 8 | + * 2. The version in package.json doesn't contain '-alpha.' (was overwritten) |
| 9 | + * 3. If so, restores the previous alpha version from git history |
| 10 | + */ |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const path = require('path'); |
| 14 | +const { execSync } = require('child_process'); |
| 15 | + |
| 16 | +const ROOT_DIR = path.resolve(__dirname, '..'); |
| 17 | +const LESS_PKG_PATH = path.join(ROOT_DIR, 'packages', 'less', 'package.json'); |
| 18 | + |
| 19 | +// Get current branch |
| 20 | +function getCurrentBranch() { |
| 21 | + try { |
| 22 | + return execSync('git rev-parse --abbrev-ref HEAD', { |
| 23 | + cwd: ROOT_DIR, |
| 24 | + encoding: 'utf8' |
| 25 | + }).trim(); |
| 26 | + } catch (e) { |
| 27 | + return null; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Read package.json version |
| 32 | +function getVersion(pkgPath) { |
| 33 | + try { |
| 34 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 35 | + return pkg.version; |
| 36 | + } catch (e) { |
| 37 | + return null; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Update version in package.json |
| 42 | +function updateVersion(pkgPath, newVersion) { |
| 43 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 44 | + pkg.version = newVersion; |
| 45 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, '\t') + '\n', 'utf8'); |
| 46 | +} |
| 47 | + |
| 48 | +// Find last alpha version from git history |
| 49 | +function findLastAlphaVersion() { |
| 50 | + try { |
| 51 | + // Get recent commits on alpha that modified package.json |
| 52 | + const commits = execSync( |
| 53 | + 'git log alpha --oneline -20 -- packages/less/package.json', |
| 54 | + { cwd: ROOT_DIR, encoding: 'utf8' } |
| 55 | + ).trim().split('\n'); |
| 56 | + |
| 57 | + // Search through commits to find the last alpha version |
| 58 | + for (const commitLine of commits) { |
| 59 | + const commitHash = commitLine.split(' ')[0]; |
| 60 | + try { |
| 61 | + const pkgContent = execSync( |
| 62 | + `git show ${commitHash}:packages/less/package.json 2>/dev/null`, |
| 63 | + { cwd: ROOT_DIR, encoding: 'utf8' } |
| 64 | + ); |
| 65 | + const pkg = JSON.parse(pkgContent); |
| 66 | + if (pkg.version && pkg.version.includes('-alpha.')) { |
| 67 | + return pkg.version; |
| 68 | + } |
| 69 | + } catch (e) { |
| 70 | + // Continue to next commit |
| 71 | + } |
| 72 | + } |
| 73 | + } catch (e) { |
| 74 | + // Ignore errors |
| 75 | + } |
| 76 | + return null; |
| 77 | +} |
| 78 | + |
| 79 | +// Update all package.json files with new version |
| 80 | +function updateAllVersions(newVersion) { |
| 81 | + const packageFiles = [ |
| 82 | + path.join(ROOT_DIR, 'package.json'), |
| 83 | + path.join(ROOT_DIR, 'packages', 'less', 'package.json'), |
| 84 | + path.join(ROOT_DIR, 'packages', 'test-data', 'package.json') |
| 85 | + ]; |
| 86 | + |
| 87 | + for (const pkgPath of packageFiles) { |
| 88 | + if (fs.existsSync(pkgPath)) { |
| 89 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 90 | + if (pkg.version) { |
| 91 | + pkg.version = newVersion; |
| 92 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, '\t') + '\n', 'utf8'); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// Main function |
| 99 | +function main() { |
| 100 | + const branch = getCurrentBranch(); |
| 101 | + |
| 102 | + // Only run on alpha branch |
| 103 | + if (branch !== 'alpha') { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const currentVersion = getVersion(LESS_PKG_PATH); |
| 108 | + |
| 109 | + if (!currentVersion) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Check if version was overwritten (doesn't contain -alpha.) |
| 114 | + if (!currentVersion.includes('-alpha.')) { |
| 115 | + console.log(`\n⚠️ Post-merge: Alpha version was overwritten (${currentVersion})`); |
| 116 | + console.log(` Attempting to restore alpha version...`); |
| 117 | + |
| 118 | + const lastAlphaVersion = findLastAlphaVersion(); |
| 119 | + |
| 120 | + if (lastAlphaVersion) { |
| 121 | + // Increment the alpha number |
| 122 | + const alphaMatch = lastAlphaVersion.match(/^(\d+\.\d+\.\d+)-alpha\.(\d+)$/); |
| 123 | + if (alphaMatch) { |
| 124 | + const alphaNum = parseInt(alphaMatch[2], 10); |
| 125 | + const newAlphaVersion = `${alphaMatch[1]}-alpha.${alphaNum + 1}`; |
| 126 | + console.log(` Restoring and incrementing: ${lastAlphaVersion} → ${newAlphaVersion}`); |
| 127 | + updateAllVersions(newAlphaVersion); |
| 128 | + console.log(`✅ Restored alpha version: ${newAlphaVersion}`); |
| 129 | + console.log(` Please commit this change: git add package.json packages/*/package.json && git commit -m "chore: restore alpha version after merge"`); |
| 130 | + } else { |
| 131 | + console.log(` Restoring to: ${lastAlphaVersion}`); |
| 132 | + updateAllVersions(lastAlphaVersion); |
| 133 | + console.log(`✅ Restored alpha version: ${lastAlphaVersion}`); |
| 134 | + console.log(` Please commit this change: git add package.json packages/*/package.json && git commit -m "chore: restore alpha version after merge"`); |
| 135 | + } |
| 136 | + } else { |
| 137 | + // No previous alpha version found, create one |
| 138 | + const parts = currentVersion.split('.'); |
| 139 | + const nextMajor = parseInt(parts[0], 10) + 1; |
| 140 | + const newAlphaVersion = `${nextMajor}.0.0-alpha.1`; |
| 141 | + console.log(` No previous alpha version found. Creating: ${newAlphaVersion}`); |
| 142 | + updateAllVersions(newAlphaVersion); |
| 143 | + console.log(`✅ Created new alpha version: ${newAlphaVersion}`); |
| 144 | + console.log(` Please commit this change: git add package.json packages/*/package.json && git commit -m "chore: restore alpha version after merge"`); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +if (require.main === module) { |
| 150 | + main(); |
| 151 | +} |
| 152 | + |
| 153 | +module.exports = { main }; |
0 commit comments