|
| 1 | +// fix-imports.js |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const SRC_DIR = path.join(__dirname, 'src'); |
| 6 | + |
| 7 | +// Recursively scan a folder |
| 8 | +function scanFolder(folder) { |
| 9 | + const files = fs.readdirSync(folder); |
| 10 | + |
| 11 | + files.forEach((file) => { |
| 12 | + const fullPath = path.join(folder, file); |
| 13 | + const stat = fs.statSync(fullPath); |
| 14 | + |
| 15 | + if (stat.isDirectory()) { |
| 16 | + scanFolder(fullPath); |
| 17 | + } else if (stat.isFile() && (file.endsWith('.js') || file.endsWith('.jsx'))) { |
| 18 | + let content = fs.readFileSync(fullPath, 'utf-8'); |
| 19 | + let updated = content; |
| 20 | + |
| 21 | + // Match import statements |
| 22 | + updated = updated.replace( |
| 23 | + /import\s+([^\n]+?)\s+from\s+['"](.+?)['"]/g, |
| 24 | + (match, imports, importPath) => { |
| 25 | + // Only update relative imports |
| 26 | + if (importPath.startsWith('.') && !importPath.endsWith('.css') && !importPath.endsWith('.json')) { |
| 27 | + const resolvedPathJs = path.resolve(path.dirname(fullPath), importPath + '.js'); |
| 28 | + if (fs.existsSync(resolvedPathJs)) { |
| 29 | + return `import ${imports} from '${importPath}.jsx'`; |
| 30 | + } |
| 31 | + } |
| 32 | + return match; |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + if (updated !== content) { |
| 37 | + fs.writeFileSync(fullPath, updated, 'utf-8'); |
| 38 | + console.log(`Updated imports in: ${fullPath}`); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +scanFolder(SRC_DIR); |
| 45 | +console.log('Done fixing imports to include .jsx.'); |
0 commit comments