|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +/** |
| 4 | + * This file resets all changes made by the init.mjs script |
| 5 | + * to restore the repository to its original React 18 state |
| 6 | + */ |
| 7 | +import { execSync } from 'child_process'; |
| 8 | +import fs from 'fs'; |
| 9 | +import path from 'path'; |
| 10 | +import { fileURLToPath } from 'url'; |
| 11 | + |
| 12 | +const __filename = fileURLToPath(import.meta.url); |
| 13 | +const __dirname = path.dirname(__filename); |
| 14 | + |
| 15 | +const ROOT_DIR = path.resolve(__dirname, '../..'); |
| 16 | + |
| 17 | +// Check for verbose flag |
| 18 | +const isVerbose = |
| 19 | + process.argv.includes('--verbose') || process.argv.includes('-v'); |
| 20 | + |
| 21 | +async function main() { |
| 22 | + console.log('🔄 Resetting React 17 environment changes'); |
| 23 | + |
| 24 | + if (isVerbose) { |
| 25 | + console.log('📝 Verbose mode enabled - showing detailed output'); |
| 26 | + } |
| 27 | + |
| 28 | + // Step 1: Reset root package.json |
| 29 | + console.log('1️⃣ Resetting package.json...'); |
| 30 | + await resetFile('package.json'); |
| 31 | + |
| 32 | + // Step 2: Reset pnpm-lock.yaml if it exists |
| 33 | + console.log('2️⃣ Resetting pnpm-lock.yaml...'); |
| 34 | + await resetFile('pnpm-lock.yaml'); |
| 35 | + |
| 36 | + // Step 3: Reset all tsconfig.json files in package directories |
| 37 | + console.log('3️⃣ Resetting TypeScript configurations...'); |
| 38 | + await resetTsConfigFiles(); |
| 39 | + |
| 40 | + console.log('✅ React 17 environment reset complete!'); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Reset a specific file using git checkout |
| 45 | + * @param {string} fileName - The file name to reset |
| 46 | + */ |
| 47 | +async function resetFile(fileName) { |
| 48 | + // Check if file is actually modified |
| 49 | + if (!isFileModified(fileName)) { |
| 50 | + if (isVerbose) { |
| 51 | + console.log(`\t ✓ ${fileName} (no changes)`); |
| 52 | + } |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + const cmd = `git checkout HEAD -- ${fileName}`; |
| 58 | + if (isVerbose) { |
| 59 | + console.log(`\t \x1b[90m${cmd}\x1b[0m`); |
| 60 | + } |
| 61 | + |
| 62 | + execSync(cmd, { |
| 63 | + cwd: ROOT_DIR, |
| 64 | + stdio: isVerbose ? 'inherit' : 'pipe', |
| 65 | + }); |
| 66 | + |
| 67 | + if (isVerbose) { |
| 68 | + console.log(`\t ✓ Reset ${fileName}`); |
| 69 | + } |
| 70 | + } catch (error) { |
| 71 | + // File might not be tracked or might not exist, which is fine |
| 72 | + console.log( |
| 73 | + `\t ⚠️ Could not reset ${fileName} (might not be tracked or might not exist)`, |
| 74 | + ); |
| 75 | + if (isVerbose) { |
| 76 | + console.log(`\t Error: ${error.message}`); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Check if a file is modified in git |
| 83 | + * @param {string} filePath - The relative path to check |
| 84 | + * @returns {boolean} True if the file is modified |
| 85 | + */ |
| 86 | +function isFileModified(filePath) { |
| 87 | + try { |
| 88 | + const output = execSync(`git status --porcelain -- "${filePath}"`, { |
| 89 | + cwd: ROOT_DIR, |
| 90 | + stdio: 'pipe', |
| 91 | + encoding: 'utf8', |
| 92 | + }); |
| 93 | + return output.trim().length > 0; |
| 94 | + } catch (error) { |
| 95 | + return false; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Get all modified tsconfig.json files |
| 101 | + * @returns {Array<string>} Array of modified tsconfig.json file paths |
| 102 | + */ |
| 103 | +function getModifiedTsConfigFiles() { |
| 104 | + try { |
| 105 | + const output = execSync('git status --porcelain', { |
| 106 | + cwd: ROOT_DIR, |
| 107 | + stdio: 'pipe', |
| 108 | + encoding: 'utf8', |
| 109 | + }); |
| 110 | + |
| 111 | + return output |
| 112 | + .split('\n') |
| 113 | + .filter(line => line.trim().length > 0) |
| 114 | + .filter(line => line.includes('tsconfig.json')) |
| 115 | + .map(line => line.slice(3).trim()); // Remove git status prefix (e.g., " M ") |
| 116 | + } catch (error) { |
| 117 | + return []; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Find and reset all tsconfig.json files in package directories |
| 123 | + */ |
| 124 | +async function resetTsConfigFiles() { |
| 125 | + const modifiedTsConfigFiles = getModifiedTsConfigFiles(); |
| 126 | + |
| 127 | + console.log( |
| 128 | + `\t Found ${modifiedTsConfigFiles.length} modified tsconfig.json files`, |
| 129 | + ); |
| 130 | + |
| 131 | + if (modifiedTsConfigFiles.length === 0) { |
| 132 | + console.log('\t ✓ No tsconfig.json files need to be reset'); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + console.log('\t Resetting modified files...'); |
| 137 | + |
| 138 | + let processed = 0; |
| 139 | + let errors = 0; |
| 140 | + |
| 141 | + for (const modifiedFile of modifiedTsConfigFiles) { |
| 142 | + try { |
| 143 | + resetFile(modifiedFile); |
| 144 | + processed++; |
| 145 | + } catch (error) { |
| 146 | + console.log(`\t ⚠️ Could not reset ${modifiedFile}`); |
| 147 | + if (isVerbose) { |
| 148 | + console.log(`\t Error: ${error.message}`); |
| 149 | + } |
| 150 | + errors++; |
| 151 | + processed++; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (errors > 0) { |
| 156 | + console.log( |
| 157 | + `\t ✓ Reset ${processed - errors} tsconfig.json files (${errors} errors)`, |
| 158 | + ); |
| 159 | + } else { |
| 160 | + console.log(`\t ✓ Reset ${processed} tsconfig.json files`); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Find tsconfig.json files only in root package directories (packages, charts, chat, tools) |
| 166 | + * Skip nested directories like scripts, src, etc. to avoid modifying non-package configs |
| 167 | + * @param {string} dir - The directory to search in |
| 168 | + * @returns {Array<string>} Array of tsconfig.json file paths |
| 169 | + */ |
| 170 | +function findTsConfigFiles(dir) { |
| 171 | + /** @type {Array<string>} */ |
| 172 | + const results = []; |
| 173 | + const packageDirs = ['packages', 'charts', 'chat', 'tools']; |
| 174 | + |
| 175 | + for (const packageDir of packageDirs) { |
| 176 | + const packagePath = path.join(dir, packageDir); |
| 177 | + |
| 178 | + if (!fs.existsSync(packagePath)) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + try { |
| 183 | + const packages = fs.readdirSync(packagePath); |
| 184 | + |
| 185 | + for (const pkg of packages) { |
| 186 | + const pkgPath = path.join(packagePath, pkg); |
| 187 | + const stat = fs.statSync(pkgPath); |
| 188 | + |
| 189 | + if (stat.isDirectory()) { |
| 190 | + const tsConfigPath = path.join(pkgPath, 'tsconfig.json'); |
| 191 | + |
| 192 | + if (fs.existsSync(tsConfigPath)) { |
| 193 | + results.push(tsConfigPath); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } catch (error) { |
| 198 | + console.warn(`⚠️ Failed to read directory ${packagePath}:`, error); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return results; |
| 203 | +} |
| 204 | + |
| 205 | +// Run the script |
| 206 | +main().catch(error => { |
| 207 | + console.error('❌ Reset script failed:', error); |
| 208 | + process.exit(1); |
| 209 | +}); |
0 commit comments