|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require("fs").promises; |
| 4 | +const path = require("path"); |
| 5 | + |
| 6 | +const extensionMap = { |
| 7 | + ".js": ".mjs", |
| 8 | + ".d.ts": ".d.mts", |
| 9 | +}; |
| 10 | +const oldExtensions = Object.keys(extensionMap); |
| 11 | + |
| 12 | +async function findFiles(rootPath) { |
| 13 | + const files = []; |
| 14 | + |
| 15 | + async function scan(directory) { |
| 16 | + const entries = await fs.readdir(directory, { withFileTypes: true }); |
| 17 | + |
| 18 | + for (const entry of entries) { |
| 19 | + const fullPath = path.join(directory, entry.name); |
| 20 | + |
| 21 | + if (entry.isDirectory()) { |
| 22 | + if (entry.name !== "node_modules" && !entry.name.startsWith(".")) { |
| 23 | + await scan(fullPath); |
| 24 | + } |
| 25 | + } else if (entry.isFile()) { |
| 26 | + if (oldExtensions.some((ext) => entry.name.endsWith(ext))) { |
| 27 | + files.push(fullPath); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + await scan(rootPath); |
| 34 | + return files; |
| 35 | +} |
| 36 | + |
| 37 | +async function updateFiles(files) { |
| 38 | + const updatedFiles = []; |
| 39 | + for (const file of files) { |
| 40 | + const updated = await updateFileContents(file); |
| 41 | + updatedFiles.push(updated); |
| 42 | + } |
| 43 | + |
| 44 | + console.log(`Updated imports in ${updatedFiles.length} files.`); |
| 45 | +} |
| 46 | + |
| 47 | +async function updateFileContents(file) { |
| 48 | + const content = await fs.readFile(file, "utf8"); |
| 49 | + |
| 50 | + let newContent = content; |
| 51 | + // Update each extension type defined in the map |
| 52 | + for (const [oldExt, newExt] of Object.entries(extensionMap)) { |
| 53 | + const regex = new RegExp(`(import|export)(.+from\\s+['"])(\\.\\.?\\/[^'"]+)(\\${oldExt})(['"])`, "g"); |
| 54 | + newContent = newContent.replace(regex, `$1$2$3${newExt}$5`); |
| 55 | + } |
| 56 | + |
| 57 | + if (content !== newContent) { |
| 58 | + await fs.writeFile(file, newContent, "utf8"); |
| 59 | + return true; |
| 60 | + } |
| 61 | + return false; |
| 62 | +} |
| 63 | + |
| 64 | +async function renameFiles(files) { |
| 65 | + let counter = 0; |
| 66 | + for (const file of files) { |
| 67 | + const ext = oldExtensions.find((ext) => file.endsWith(ext)); |
| 68 | + const newExt = extensionMap[ext]; |
| 69 | + |
| 70 | + if (newExt) { |
| 71 | + const newPath = file.slice(0, -ext.length) + newExt; |
| 72 | + await fs.rename(file, newPath); |
| 73 | + counter++; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + console.log(`Renamed ${counter} files.`); |
| 78 | +} |
| 79 | + |
| 80 | +async function main() { |
| 81 | + try { |
| 82 | + const targetDir = process.argv[2]; |
| 83 | + if (!targetDir) { |
| 84 | + console.error("Please provide a target directory"); |
| 85 | + process.exit(1); |
| 86 | + } |
| 87 | + |
| 88 | + const targetPath = path.resolve(targetDir); |
| 89 | + const targetStats = await fs.stat(targetPath); |
| 90 | + |
| 91 | + if (!targetStats.isDirectory()) { |
| 92 | + console.error("The provided path is not a directory"); |
| 93 | + process.exit(1); |
| 94 | + } |
| 95 | + |
| 96 | + console.log(`Scanning directory: ${targetDir}`); |
| 97 | + |
| 98 | + const files = await findFiles(targetDir); |
| 99 | + |
| 100 | + if (files.length === 0) { |
| 101 | + console.log("No matching files found."); |
| 102 | + process.exit(0); |
| 103 | + } |
| 104 | + |
| 105 | + console.log(`Found ${files.length} files.`); |
| 106 | + await updateFiles(files); |
| 107 | + await renameFiles(files); |
| 108 | + console.log("\nDone!"); |
| 109 | + } catch (error) { |
| 110 | + console.error("An error occurred:", error.message); |
| 111 | + process.exit(1); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +main(); |
0 commit comments