|
| 1 | +import { readdirSync, readFileSync, writeFileSync, existsSync } from 'fs'; |
| 2 | +import { resolve, dirname, relative } from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | +const __dirname = dirname(__filename); |
| 7 | +const rootDir = resolve(__dirname, '..'); |
| 8 | + |
| 9 | +function walkDir(dir, callback) { |
| 10 | + readdirSync(dir, { withFileTypes: true }).forEach((entry) => { |
| 11 | + const path = resolve(dir, entry.name); |
| 12 | + if (entry.isDirectory()) { |
| 13 | + walkDir(path, callback); |
| 14 | + } else if (entry.name.endsWith('.d.ts')) { |
| 15 | + callback(path); |
| 16 | + } |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +function fixPaths() { |
| 21 | + const distDir = resolve(rootDir, 'dist'); |
| 22 | + |
| 23 | + walkDir(distDir, (filePath) => { |
| 24 | + let content = readFileSync(filePath, 'utf-8'); |
| 25 | + let modified = content.replace(/from '@\/([^']+)'/g, (match, path) => { |
| 26 | + const cleanPath = path.replace(/^@/, ''); |
| 27 | + let targetPath = resolve(distDir, cleanPath + '.d.ts'); |
| 28 | + if (!existsSync(targetPath)) { |
| 29 | + targetPath = resolve(distDir, cleanPath); |
| 30 | + } |
| 31 | + const relPath = './' + relative(dirname(filePath), targetPath).replace('.d.ts', ''); |
| 32 | + return `from '${relPath}'`; |
| 33 | + }); |
| 34 | + if (modified !== content) { |
| 35 | + writeFileSync(filePath, modified); |
| 36 | + console.log('Fixed:', filePath); |
| 37 | + } |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +fixPaths(); |
0 commit comments