Spaces:
Paused
Paused
| 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() | |