import subprocess import sys import os def format_files(files_str): """ Aplica formatação automática usando ferramentas nativas (npx prettier / black). Uso: python code_formatter.py "arquivo1.js arquivo2.py" """ files = files_str.split() results = [] for file_path in files: if not os.path.exists(file_path): results.append(f"⚠️ Ignorado (não existe): {file_path}") continue try: # Estratégia JS/TS/HTML/CSS (Prettier) if file_path.endswith(('.js', '.ts', '.jsx', '.html', '.css', '.json', '.md')): # Tenta usar npx -y prettier cmd = f"npx -y prettier --write {file_path}" subprocess.run(cmd, shell=True, check=True, capture_output=True) results.append(f"✨ Formatado (Prettier): {file_path}") # Estratégia Python (Black) elif file_path.endswith('.py'): cmd = f"python -m black {file_path}" subprocess.run(cmd, shell=True, check=True, capture_output=True) results.append(f"🐍 Formatado (Black): {file_path}") else: results.append(f"⚪ Sem formatador configurado: {file_path}") except subprocess.CalledProcessError: results.append(f"❌ Erro ao formatar: {file_path} (Verifique sintaxe)") return "\n".join(results) if __name__ == "__main__": if len(sys.argv) < 2: print("Uso: python code_formatter.py \"src/app.js src/utils.py\"") else: # Junta todos os argumentos em uma string de arquivos print(format_files(" ".join(sys.argv[1:])))