Spaces:
Sleeping

File size: 2,115 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from "fs";
import path from "path";
import dotenv from "dotenv";

async function syncEnv() {
    const envPath = path.resolve(process.cwd(), ".env");

    if (!fs.existsSync(envPath)) {
        console.error("❌ .env file not found at:", envPath);
        process.exit(1);
    }

    // Load .env content
    const envConfig = dotenv.parse(fs.readFileSync(envPath));
    const hfToken = process.env.HF_TOKEN;

    if (!hfToken) {
        console.error("❌ HF_TOKEN environment variable is missing.");
        console.log("Please run: export HF_TOKEN=your_token (or set it in your environment)");
        process.exit(1);
    }

    const repoId = "fouadkhalid123/asd";
    const apiUrl = `https://huggingface.co/api/spaces/${repoId}/secrets`;

    console.log(`πŸš€ Syncing ${Object.keys(envConfig).length} variables to Hugging Face Space: ${repoId}...`);

    for (const [key, value] of Object.entries(envConfig)) {
        if (!value) continue;

        try {
            console.log(`πŸ“€ Syncing: ${key}...`);
            const response = await fetch(apiUrl, {
                method: "POST",
                headers: {
                    "Authorization": `Bearer ${hfToken}`,
                    "Content-Type": "application/json",
                    "User-Agent": "HF-Sync-Script/1.0",
                },
                body: JSON.stringify({ key, value }),
            });

            const responseData = await response.json().catch(() => ({}));

            if (!response.ok) {
                console.error(`❌ Failed to sync ${key}:`, response.status, responseData);
                // 403 often means missing scope or wrong headers
                if (response.status === 403) {
                    console.error("πŸ’‘ TIP: Verify your HF_TOKEN has 'Write' access to this specific Space and its Secrets.");
                }
            } else {
                console.log(`βœ… ${key} synced successfully.`);
            }
        } catch (error) {
            console.error(`❌ Error syncing ${key}:`, error);
        }
    }

    console.log("\n✨ Sync completed!");
}

syncEnv();