|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +function handleFile(filePath) { |
| 5 | + fs.readFile(filePath, 'utf-8', (error, content) => { |
| 6 | + if (error) { |
| 7 | + console.error('Error reading the file:', error); |
| 8 | + return; |
| 9 | + } |
| 10 | + const split = content.split('\n'); |
| 11 | + let changed = false; |
| 12 | + const newSplit = split.map((line) => { |
| 13 | + if (!line.startsWith('import') || line.endsWith(".js';") || line.includes("from 'cesium")) { |
| 14 | + return line; |
| 15 | + } |
| 16 | + let newline = undefined; |
| 17 | + if (line.endsWith(".ts';")) { |
| 18 | + newline = line.replace(".ts';", ".js';"); |
| 19 | + } else if (line.includes("from './")) { |
| 20 | + newline = line.replace("';", ".js';"); |
| 21 | + } else { |
| 22 | + console.error('XX', filePath, line); |
| 23 | + throw new Error('pb', line); |
| 24 | + } |
| 25 | + console.log('changed:', line, '->', newline); |
| 26 | + changed = true; |
| 27 | + return newline; |
| 28 | + }); |
| 29 | + if (changed) { |
| 30 | + fs.writeFileSync(filePath, newSplit.join('\n'), { |
| 31 | + encoding: 'utf-8' |
| 32 | + }); |
| 33 | + console.log('wrote', filePath); |
| 34 | + } |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function listFilesRecursively(dir) { |
| 39 | + const files = fs.readdirSync(dir); |
| 40 | + |
| 41 | + files.forEach((file) => { |
| 42 | + const filePath = path.join(dir, file); |
| 43 | + |
| 44 | + const stat = fs.statSync(filePath); |
| 45 | + |
| 46 | + if (stat.isFile()) { |
| 47 | + handleFile(filePath); |
| 48 | + } else if (stat.isDirectory()) { |
| 49 | + listFilesRecursively(filePath); |
| 50 | + } |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +const startingDirectory = process.argv[2]; |
| 55 | +listFilesRecursively(startingDirectory); |
0 commit comments