Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save danyelhenrique/dd3fa36d4788701488db5b4f7ebed6e0 to your computer and use it in GitHub Desktop.

Select an option

Save danyelhenrique/dd3fa36d4788701488db5b4f7ebed6e0 to your computer and use it in GitHub Desktop.
change folder and sub file extensions
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