|
| 1 | +#!/usr/bin/env ts-node |
| 2 | + |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import { execSync } from 'child_process'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Apply override commits to the mastodon-documentation repository |
| 9 | + */ |
| 10 | +function applyOverrides(): void { |
| 11 | + const configPath = path.join(__dirname, '..', 'config.json'); |
| 12 | + const docsDir = path.join(__dirname, '..', 'mastodon-documentation'); |
| 13 | + |
| 14 | + if (!fs.existsSync(configPath)) { |
| 15 | + console.error('config.json not found'); |
| 16 | + process.exit(1); |
| 17 | + } |
| 18 | + |
| 19 | + if (!fs.existsSync(docsDir)) { |
| 20 | + console.error( |
| 21 | + 'mastodon-documentation directory not found. Run setup-docs first.' |
| 22 | + ); |
| 23 | + process.exit(1); |
| 24 | + } |
| 25 | + |
| 26 | + const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); |
| 27 | + const overrideCommits = config.overrideCommits || []; |
| 28 | + const overridesRepository = config.overridesRepository; |
| 29 | + |
| 30 | + // Exit early if no overrides to apply |
| 31 | + if (overrideCommits.length === 0) { |
| 32 | + console.log('No override commits to apply.'); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + console.log(`Applying ${overrideCommits.length} override commit(s)...`); |
| 37 | + |
| 38 | + try { |
| 39 | + // Configure git user for cherry-picking |
| 40 | + console.log('Configuring git user...'); |
| 41 | + execSync(`git config user.email "[email protected]"`, { |
| 42 | + stdio: 'inherit', |
| 43 | + cwd: docsDir, |
| 44 | + }); |
| 45 | + execSync(`git config user.name "bot"`, { |
| 46 | + stdio: 'inherit', |
| 47 | + cwd: docsDir, |
| 48 | + }); |
| 49 | + |
| 50 | + // Add overrides remote if repository is specified |
| 51 | + if (overridesRepository) { |
| 52 | + console.log(`Adding ${overridesRepository} overrides remote...`); |
| 53 | + execSync(`git remote add overrides ${overridesRepository} || true`, { |
| 54 | + stdio: 'inherit', |
| 55 | + cwd: docsDir, |
| 56 | + }); |
| 57 | + execSync(`git fetch overrides`, { |
| 58 | + stdio: 'inherit', |
| 59 | + cwd: docsDir, |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + // Apply each override commit |
| 64 | + for (const commit of overrideCommits) { |
| 65 | + console.log(`Cherry-picking commit ${commit}...`); |
| 66 | + execSync(`git cherry-pick ${commit}`, { |
| 67 | + stdio: 'inherit', |
| 68 | + cwd: docsDir, |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + console.log('Override commits applied successfully.'); |
| 73 | + } catch (error) { |
| 74 | + console.error('Error applying override commits:', (error as Error).message); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +if (require.main === module) { |
| 80 | + applyOverrides(); |
| 81 | +} |
| 82 | + |
| 83 | +export { applyOverrides }; |
0 commit comments