Created
January 24, 2024 02:20
-
-
Save rafael-angonese/b43b6c1a56f95a0f7bbc15f2ce178f8b to your computer and use it in GitHub Desktop.
rename files directories and update imports into directory
This file contains 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 convertCamelToKebabCase(str) { | |
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); | |
} | |
function renameFilesAndDirectories(directoryPath) { | |
fs.readdirSync(directoryPath).forEach(item => { | |
const itemPath = path.join(directoryPath, item); | |
const isDirectory = fs.statSync(itemPath).isDirectory(); | |
const kebabCaseName = convertCamelToKebabCase(item); | |
if (isDirectory) { | |
// Se for um diretório, renomeia para kebab-case e chama recursivamente a função | |
const newDirectoryPath = path.join(directoryPath, kebabCaseName); | |
fs.renameSync(itemPath, newDirectoryPath); | |
console.log(`Renomeando diretório: ${itemPath} para ${newDirectoryPath}`); | |
renameFilesAndDirectories(newDirectoryPath); | |
} else { | |
// Se for um arquivo, renomeia para kebab-case | |
const newFilePath = path.join(directoryPath, kebabCaseName); | |
fs.renameSync(itemPath, newFilePath); | |
console.log(`Renomeando arquivo: ${itemPath} para ${newFilePath}`); | |
} | |
}); | |
} | |
// Substitua 'caminho/do/seu/diretorio' pelo caminho do diretório desejado | |
const directoryPath = '/home/rafa/workspaces/diero/frontend/src'; | |
renameFilesAndDirectories(directoryPath); | |
console.log('Conversão para kebab-case concluída.'); |
This file contains 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 convertCamelToKebabCase(str) { | |
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); | |
} | |
function updateImportPaths(filePath) { | |
const fileContent = fs.readFileSync(filePath, 'utf-8'); | |
const updatedContent = fileContent.replace( | |
/((@\/)|(\.\.?\/|\.\/))(@?[.\w\/-]+)(['])/g, | |
(_, prefix, atPrefix, relativePrefix, match, quote) => { | |
if (atPrefix) { | |
const convertedMatch = convertCamelToKebabCase(match.replace(/^@/, '')); | |
return `${prefix}${convertedMatch}${quote}`; | |
} else { | |
const convertedMatch = convertCamelToKebabCase(match); | |
return `${prefix}${convertedMatch}${quote}`; | |
} | |
} | |
); | |
fs.writeFileSync(filePath, updatedContent); | |
} | |
function updateImportsInDirectory(directoryPath) { | |
fs.readdirSync(directoryPath).forEach(item => { | |
const itemPath = path.join(directoryPath, item); | |
const isDirectory = fs.statSync(itemPath).isDirectory(); | |
if (isDirectory) { | |
// Se for um diretório, chama recursivamente a função | |
updateImportsInDirectory(itemPath); | |
} else { | |
// Se for um arquivo .ts ou .tsx, atualiza as importações | |
if (path.extname(itemPath) === '.ts' || path.extname(itemPath) === '.tsx') { | |
updateImportPaths(itemPath); | |
console.log(`Atualizando importações em: ${itemPath}`); | |
} | |
} | |
}); | |
} | |
// Substitua 'caminho/do/seu/diretorio' pelo caminho do diretório desejado | |
const directoryPath = '/home/rafa/workspaces/diero/frontend/src'; | |
updateImportsInDirectory(directoryPath); | |
console.log('Atualização das importações concluída.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment