Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Created January 4, 2023 19:31
Show Gist options
  • Save VitorLuizC/d1311974fa75098693c14a951d284969 to your computer and use it in GitHub Desktop.
Save VitorLuizC/d1311974fa75098693c14a951d284969 to your computer and use it in GitHub Desktop.
// @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
// @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