Skip to content

Instantly share code, notes, and snippets.

@manzt
Last active August 9, 2022 13:37
Show Gist options
  • Save manzt/8d85c605bedcdb1f839f1933cd87e595 to your computer and use it in GitHub Desktop.
Save manzt/8d85c605bedcdb1f839f1933cd87e595 to your computer and use it in GitHub Desktop.
rename .js -> .jsx for JS files containing JSX
// @ts-check
import * as fsp from "node:fs/promises";
import * as util from "node:util";
import glob from "glob";
const matchingReactString = "from 'react'";
const BLUE = "\u001b[34m";
const YELLOW = "\u001b[33m";
const RESET = "\u001b[0m";
let matches = await util.promisify(glob)(process.argv[2]);
console.log(`${BLUE}Found these files matching the globs specified:${RESET}`);
console.log(matches);
matches.forEach(async (path) => {
const result = await fsp.readFile(path);
if (!result.includes(matchingReactString)) return;
// Just add 'x' to the end to make it 'jsx'
const newPath = path.concat("x");
console.log(`${YELLOW}Renaming ${path} to ${newPath}${RESET}`);
fsp.rename(path, newPath);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment