|
| 1 | +#!/usr/bin/env node |
| 2 | +// Script to add .js extensions to all relative imports in TypeScript files |
| 3 | +// This is required for ESM compatibility |
| 4 | + |
| 5 | +import { promises as fs } from 'fs'; |
| 6 | +import path from 'path'; |
| 7 | +import { fileURLToPath } from 'url'; |
| 8 | +import { dirname } from 'path'; |
| 9 | + |
| 10 | +const __filename = fileURLToPath(import.meta.url); |
| 11 | +const __dirname = dirname(__filename); |
| 12 | + |
| 13 | +const rootDir = path.join(__dirname, '..'); |
| 14 | +const srcDir = path.join(rootDir, 'src'); |
| 15 | + |
| 16 | +// Regex patterns to match import statements with relative paths |
| 17 | +const importPatterns = [ |
| 18 | + // import ... from './path' or '../path' |
| 19 | + /^(\s*import\s+.+\s+from\s+['"])(\.\/.+?|\.\.?\/.+?)(['"])/gm, |
| 20 | + // export ... from './path' or '../path' |
| 21 | + /^(\s*export\s+.+\s+from\s+['"])(\.\/.+?|\.\.?\/.+?)(['"])/gm, |
| 22 | + // import('./path') or import('../path') |
| 23 | + /(\bimport\s*\(\s*['"])(\.\/.+?|\.\.?\/.+?)(['"])/gm, |
| 24 | + // await import('./path') |
| 25 | + /(\bawait\s+import\s*\(\s*['"])(\.\/.+?|\.\.?\/.+?)(['"])/gm, |
| 26 | +]; |
| 27 | + |
| 28 | +async function getAllTsFiles(dir) { |
| 29 | + const files = []; |
| 30 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 31 | + |
| 32 | + for (const entry of entries) { |
| 33 | + const fullPath = path.join(dir, entry.name); |
| 34 | + |
| 35 | + if (entry.isDirectory()) { |
| 36 | + // Skip node_modules, out, dist, etc. |
| 37 | + if (!['node_modules', 'out', 'dist', '.git', '.vscode', 'resources'].includes(entry.name)) { |
| 38 | + files.push(...(await getAllTsFiles(fullPath))); |
| 39 | + } |
| 40 | + } else if (entry.name.endsWith('.ts') || entry.name.endsWith('.tsx')) { |
| 41 | + files.push(fullPath); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return files; |
| 46 | +} |
| 47 | + |
| 48 | +function addJsExtension(content) { |
| 49 | + let modified = content; |
| 50 | + let changeCount = 0; |
| 51 | + |
| 52 | + for (const pattern of importPatterns) { |
| 53 | + modified = modified.replace(pattern, (match, before, importPath, after) => { |
| 54 | + // Skip if already has an extension |
| 55 | + if (/\.(js|ts|tsx|json|css|less|svg|png|jpg)$/i.test(importPath)) { |
| 56 | + return match; |
| 57 | + } |
| 58 | + |
| 59 | + changeCount++; |
| 60 | + return `${before}${importPath}.js${after}`; |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + return { content: modified, changed: changeCount > 0, changeCount }; |
| 65 | +} |
| 66 | + |
| 67 | +async function main() { |
| 68 | + console.log('🔍 Finding all TypeScript files in src/...'); |
| 69 | + const tsFiles = await getAllTsFiles(srcDir); |
| 70 | + console.log(`📁 Found ${tsFiles.length} TypeScript files\n`); |
| 71 | + |
| 72 | + let totalFilesChanged = 0; |
| 73 | + let totalImportsChanged = 0; |
| 74 | + |
| 75 | + for (const file of tsFiles) { |
| 76 | + const content = await fs.readFile(file, 'utf-8'); |
| 77 | + const { content: newContent, changed, changeCount } = addJsExtension(content); |
| 78 | + |
| 79 | + if (changed) { |
| 80 | + await fs.writeFile(file, newContent, 'utf-8'); |
| 81 | + totalFilesChanged++; |
| 82 | + totalImportsChanged += changeCount; |
| 83 | + const relativePath = path.relative(rootDir, file); |
| 84 | + console.log(`✅ ${relativePath} (${changeCount} import${changeCount > 1 ? 's' : ''})`); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + console.log(`\n✨ Done!`); |
| 89 | + console.log(`📊 Modified ${totalFilesChanged} files`); |
| 90 | + console.log(`🔗 Updated ${totalImportsChanged} import statements`); |
| 91 | +} |
| 92 | + |
| 93 | +main().catch(error => { |
| 94 | + console.error('❌ Error:', error); |
| 95 | + process.exit(1); |
| 96 | +}); |
0 commit comments