Skip to content

Instantly share code, notes, and snippets.

@pwndev
Created January 17, 2023 12:42
Show Gist options
  • Save pwndev/8d163e0ec94b13b53c4d54a432cc3682 to your computer and use it in GitHub Desktop.
Save pwndev/8d163e0ec94b13b53c4d54a432cc3682 to your computer and use it in GitHub Desktop.
Simple script to rename React .js files that render jsx to .jsx
import os
import re
root_dir = "src"
JSX_REGEX = r"<\w+"
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith(".js"):
filepath = os.path.join(dirpath, filename)
with open(filepath, "r") as f:
file_contents = f.read()
if re.search(JSX_REGEX, file_contents):
new_filename = filename.replace(".js", ".jsx")
new_filepath = os.path.join(dirpath, new_filename)
os.rename(filepath, new_filepath)
print(f'{filename} has been renamed to {new_filename}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment