summaryrefslogtreecommitdiff
path: root/scripts/spin.js
blob: 7b9411ec3e556d0c94bc7101174b432f4a69ecff (plain)
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
#!/usr/bin/env node

const winner = process.argv[2];

if (!winner) {
  console.error("Usage: npm run spin -- \"Winner Name\"");
  process.exit(1);
}

const endpoint = process.env.WHEEL_URL || "http://localhost:3000";

const main = async () => {
  const response = await fetch(`${endpoint}/api/spin`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ winner }),
  });

  const data = await response.json();

  if (!response.ok) {
    console.error("Spin failed:", data.error || "unknown error");
    if (Array.isArray(data.entries)) {
      console.error("Available entries:", data.entries.join(", "));
    }
    process.exit(1);
  }

  console.log(`Spinning wheel. Winner set to: ${data.spin.winner}`);
};

main().catch((error) => {
  console.error("Request failed:", error.message);
  process.exit(1);
});