Created
December 17, 2023 21:30
-
-
Save christianvmm/38c938c592a016955edace7f96051d91 to your computer and use it in GitHub Desktop.
Change JS to JSX file extension script
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
| 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