File size: 2,411 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

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()