Last active
June 9, 2023 01:21
-
-
Save danyelhenrique/dd3fa36d4788701488db5b4f7ebed6e0 to your computer and use it in GitHub Desktop.
change folder and sub file extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require("fs"); | |
| const path = require("path"); | |
| function changeFileExtension(filePath, fileExtension) { | |
| const fileExt = path.extname(filePath); | |
| const newFilePath = path.join( | |
| path.dirname(filePath), | |
| path.basename(filePath, fileExt) + ".ts" | |
| ); | |
| fs.rename(filePath, newFilePath, (err) => { | |
| if (err) { | |
| console.error(`Fail to rename file ${filePath}:`, err); | |
| return; | |
| } | |
| console.log( | |
| `File ${filePath} renamed to ${path.basename( | |
| filePath, | |
| fileExt | |
| )}${fileExtension}` | |
| ); | |
| }); | |
| } | |
| function processDirectory(directoryPath, fileExtension) { | |
| fs.readdir(directoryPath, (err, files) => { | |
| if (err) { | |
| console.error("Erro on read directory:", err); | |
| return; | |
| } | |
| files.forEach((file) => { | |
| const filePath = path.join(directoryPath, file); | |
| fs.stat(filePath, (err, stats) => { | |
| if (err) { | |
| console.error("Erro to get file info:", err); | |
| return; | |
| } | |
| if (stats.isFile()) { | |
| changeFileExtension(filePath, fileExtension); | |
| } else if (stats.isDirectory()) { | |
| processDirectory(filePath, fileExtension); | |
| } | |
| }); | |
| }); | |
| }); | |
| } | |
| const rootDirectory = "./src/theme"; | |
| const fileExtension = ".ts"; | |
| processDirectory(rootDirectory, fileExtension); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment