custom_ui/custom_ui/commands.py

59 lines
No EOL
2.1 KiB
Python

import click
import os
import subprocess
import frappe
from custom_ui.utils import create_module
@click.command("build-frontend")
@click.option("--site", default=None, help="Site to build frontend for")
def build_frontend(site):
app_package_path = frappe.get_app_path("custom_ui")
app_root = os.path.dirname(app_package_path)
candidates = [
os.path.join(app_root, 'frontend'),
os.path.join(app_package_path, 'frontend')
]
frontend_path = None
for p in candidates:
if os.path.exists(p):
frontend_path = p
break
if frontend_path:
click.echo("\n📦 Building frontend for custom_ui...\n")
try:
subprocess.check_call(["npm", "install"], cwd=frontend_path)
subprocess.check_call(["npm", "run", "build"], cwd=frontend_path)
click.echo("\n✅ Frontend build completed successfully.\n")
except subprocess.CalledProcessError as e:
click.echo(f"\n❌ Frontend build failed: {e}\n")
exit(1)
else:
frappe.log_error(message="No frontend directory found for custom_ui", title="Frontend Build Skipped")
click.echo(f"\n⚠️ Frontend directory does not exist. Skipping build. Path was {frontend_path}\n")
if not site:
return
try:
print(f"\n🧹 Clearing cache for site {site}...\n")
subprocess.check_call(["bench", "--site", site, "clear-cache"])
subprocess.check_call(["bench", "--site", site, "clear-website-cache"])
except subprocess.CalledProcessError as e:
frappe.log_error(message=str(e), title="Clear Cache Failed")
print(f"\n❌ Clearing cache failed: {e}\n")
# Restart bench
try:
print("\n🔄 Restarting bench...\n")
subprocess.check_call(["bench", "restart"])
except subprocess.CalledProcessError as e:
frappe.log_error(message=str(e), title="Bench Restart Failed")
print(f"\n❌ Bench restart failed: {e}\n")
@click.command("create-module")
def create_module_command():
create_module()
click.echo("✅ Custom UI module created or already exists.")
commands = [build_frontend, create_module_command]