Created
January 4, 2023 19:31
-
-
Save VitorLuizC/d1311974fa75098693c14a951d284969 to your computer and use it in GitHub Desktop.
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
// @ts-check | |
import glob from 'glob' | |
/** | |
* @param {string} path | |
* @returns {Promise<string[]>} | |
*/ | |
function getFiles(path) { | |
return new Promise((resolve, reject) => { | |
glob(path, (error, files) => { | |
if (error) { | |
reject(error) | |
return | |
} | |
resolve(files) | |
}) | |
}) | |
} | |
export default getFiles |
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
// @ts-check | |
import { exec } from 'node:child_process' | |
import { readFile } from 'node:fs/promises' | |
import getFiles from './getFiles.mjs' | |
const paths = await getFiles('./src/**/*.j{s,sx}') | |
/** | |
* @param {string} content | |
* @returns {boolean} | |
*/ | |
function hasJSXExpression(content) { | |
const result = /<(\w*)>/gm.exec(content) | |
if (result === null) return false | |
const [, elementOrComponentName] = result | |
return new RegExp(`</${elementOrComponentName ?? ''}>`).test(content) | |
} | |
for (const path of paths) { | |
const content = await readFile(path, 'utf-8') | |
await new Promise((resolve, reject) => { | |
const newPath = path.replace( | |
/\.js$/, | |
hasJSXExpression(content) ? '.tsx' : '.ts' | |
) | |
exec(`git mv "${path}" "${newPath}"`, (error) => { | |
if (error) { | |
reject(error) | |
return | |
} | |
resolve(0) | |
}) | |
}) | |
console.log(path, 'was renamed.') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment