import os import subprocess from huggingface_hub import HfApi, create_repo HF_TOKEN = os.environ.get("HF_TOKEN") REPO_ID = "Kraft102/widgetdc-frontend" FRONTEND_DIR = "apps/matrix-frontend" DIST_DIR = f"{FRONTEND_DIR}/dist" ENV_FILE = f"{FRONTEND_DIR}/.env.production" def main(): print(f"🚀 Starting Build & Deploy for {REPO_ID}...") # 0. Write .env.production print("📝 Configuring environment...") env_content = ( "VITE_API_URL=https://kraft102-widgetdc-cortex.hf.space\n" "VITE_WS_URL=wss://kraft102-widgetdc-cortex.hf.space\n" "VITE_ENV=production\n" ) with open(ENV_FILE, "w", encoding="utf-8") as f: f.write(env_content) print(f"✅ Written {ENV_FILE}") # 1. Build Frontend print("🔨 Building Frontend...") try: # Install dependencies if needed (optional, can be skipped if node_modules exists, but safer to run) # subprocess.run(["npm", "install"], cwd=FRONTEND_DIR, check=True, shell=True) # Run build subprocess.run(["npm", "run", "build"], cwd=FRONTEND_DIR, check=True, shell=True) print("✅ Build Successful") except subprocess.CalledProcessError as e: print(f"❌ Build Failed: {e}") exit(1) api = HfApi(token=HF_TOKEN) # 2. Create Repo (if not exists) try: url = create_repo( repo_id=REPO_ID, token=HF_TOKEN, repo_type="space", space_sdk="static", exist_ok=True, private=False ) print(f"✅ Repo ready: {url}") except Exception as e: print(f"⚠️ Repo creation warning (might exist): {e}") # 3. Check if build exists if not os.path.exists(DIST_DIR): print(f"❌ Build directory {DIST_DIR} not found! Did build fail?") exit(1) # 4. Upload Folder print("📤 Uploading 'dist' folder...") try: api.upload_folder( folder_path=DIST_DIR, repo_id=REPO_ID, repo_type="space", path_in_repo=".", # root of space token=HF_TOKEN, commit_message="Deploy Frontend v1.1 (Auto-Build)" ) print("✅ Upload Complete!") print(f"🌍 Live URL: https://huggingface.co/spaces/{REPO_ID}") except Exception as e: print(f"❌ Upload Failed: {e}") if __name__ == "__main__": main()