Skip to content

Instantly share code, notes, and snippets.

@christianvmm
Created December 17, 2023 21:30
Show Gist options
  • Select an option

  • Save christianvmm/38c938c592a016955edace7f96051d91 to your computer and use it in GitHub Desktop.

Select an option

Save christianvmm/38c938c592a016955edace7f96051d91 to your computer and use it in GitHub Desktop.
Change JS to JSX file extension script
import fs from 'fs/promises'
import path from 'path'
const ignoredFiles = [
'.git',
'.vscode',
'node_modules',
'public'
]
function ignoreFile(filename) {
return !ignoredFiles.includes(filename)
}
const jsxPattern = /<\s*[\w.]*[^>]*>/;
async function processjsFile(dirname) {
const fileContent = await fs.readFile(dirname, 'utf-8')
if(jsxPattern.test(fileContent)) {
const jsFile = path.basename(dirname)
const jsxFile = jsFile.replace('.js', '.jsx')
const newDirName = dirname.replace(jsFile, jsxFile)
await fs.rename(dirname, newDirName)
}
}
async function readFiles(dirname) {
let files = await fs.readdir(dirname)
files = files.filter(ignoreFile)
for (const file of files) {
try {
const currentPath = `${dirname}/${file}`
const stat = await fs.lstat(currentPath)
if (stat.isDirectory()) {
await readFiles(currentPath)
} else if(stat.isFile() && file.includes('js')) {
await processjsFile(currentPath)
}
} catch {}
}
}
await readFiles('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment