Last active
February 26, 2025 18:43
-
-
Save amon-ra/1c10460508e34539d616921d1d88cf81 to your computer and use it in GitHub Desktop.
Decompiles javascript back to typescript
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 os | |
import sys | |
import time | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.firefox.service import Service | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.support.ui import WebDriverWait | |
def convert_js_to_ts(driver, js_code): | |
driver.get("https://js2ts.com/") | |
wait = WebDriverWait(driver, 4) | |
print("Left editor") | |
left_textarea = wait.until(EC.presence_of_element_located((By.XPATH, "(//textarea)[1]"))) | |
left_textarea.clear() | |
left_textarea.send_keys(js_code) | |
# Buscamos el botón "Convertir" y lo pulsamos | |
convert_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Conver')]"))) | |
convert_button.click() | |
print("Convirtiendo...") | |
# Se espera un tiempo prudente para que la conversión se complete (se puede ajustar o esperar a alguna condición) | |
time.sleep(2) | |
# Volvemos a obtener la lista de editores en caso de que el DOM se haya actualizado | |
right_textarea = wait.until(EC.presence_of_element_located((By.XPATH, "(//textarea)[2]"))) | |
editors = driver.find_elements(By.CSS_SELECTOR, ".ace_editor") | |
if len(editors) < 2: | |
raise Exception("No se encontró el editor de salida Ace después de la conversión.") | |
# Seleccionamos el editor de salida (segundo Ace editor) | |
right_editor = driver.find_elements(By.CSS_SELECTOR, ".ace_editor")[1] | |
# Esperamos a que se actualice el contenido del editor de salida (que no esté vacío) | |
wait.until(lambda d: driver.execute_script("return ace.edit(arguments[0]).getValue();", right_editor).strip() != "") | |
ts_code = driver.execute_script("return ace.edit(arguments[0]).getValue();", right_editor) | |
return ts_code | |
def main(source, dst): | |
chrome_options = Options() | |
chrome_options.add_argument("--headless") | |
# driver = webdriver.Chrome(options=chrome_options) | |
firefox_service = Service(executable_path="/usr/bin/geckodriver") | |
driver = webdriver.Firefox(service=firefox_service) | |
directorio = source | |
for root, dirs, files in os.walk(directorio): | |
for file in files: | |
if file.endswith(".js"): | |
file_path = os.path.join(root, file) | |
base_name = os.path.splitext(file)[0] | |
relative_path = os.path.relpath(root, source) | |
new_dir = os.path.join(dst, relative_path) | |
new_file = base_name + ".ts" | |
new_path = os.path.join(new_dir, new_file) | |
print(file_path) | |
if os.path.exists(new_path): | |
print(f"Ya existe: {new_path}") | |
continue | |
with open(file_path, encoding="utf-8") as f: | |
js_code = f.read() | |
ts_code = "" | |
# Verificar que el número de líneas sea mayor a 3 | |
if len(js_code.splitlines()) <= 3: | |
print(f"El archivo {file_path} tiene 3 líneas o menos. Se omite la conversión.") | |
else: | |
try: | |
ts_code = convert_js_to_ts(driver, js_code) | |
except Exception as e: | |
print(f"------------Error al convertir {file_path}: ---------------") | |
print(e) | |
continue | |
os.makedirs(new_dir, exist_ok=True) | |
with open(new_path, "w", encoding="utf-8") as f: | |
f.write(ts_code) | |
print(f"Convertido: {file_path} -> {new_path}") | |
driver.quit() | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
print("Uso: python convert.py <directorio_fuente> <directorio_destino>") | |
sys.exit(1) | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: convert.py source_dir dst_dir