forked from ryo-ma/github-profile-trophy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_svg.ts
More file actions
66 lines (54 loc) · 1.99 KB
/
render_svg.ts
File metadata and controls
66 lines (54 loc) · 1.99 KB
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
import "https://deno.land/x/dotenv@v0.5.0/load.ts";
const username = Deno.args[0];
const outputPath = Deno.args[1] ?? "./assets/trophy.svg";
const gameTheme = Deno.args[2] ?? "lol";
const apiUrl = Deno.args[3]; // Catching the new Vercel URL argument!
if (!username || !apiUrl) {
console.error(
"Usage: deno run --allow-net --allow-env --allow-read --allow-write ./render_svg.ts USERNAME OUTPUT_PATH GAME_THEME API_URL",
);
Deno.exit(1);
}
async function main() {
console.log("Starting trophy fetch...");
console.log("Username:", username);
console.log("Output path:", outputPath);
console.log("Game theme:", gameTheme);
console.log("Vercel API URL:", apiUrl);
// Clean the URL in case the user accidentally added a trailing slash
const cleanApiUrl = apiUrl.replace(/\/$/, "");
// Construct the exact URL to fetch from your Vercel deployment
const fetchUrl = `${cleanApiUrl}/?username=${username}&theme=${gameTheme}`;
console.log(`Fetching generated SVG from: ${fetchUrl}`);
try {
const response = await fetch(fetchUrl);
if (!response.ok) {
console.error(
`Failed to fetch SVG. Status: ${response.status} ${response.statusText}`,
);
const errorText = await response.text();
console.error("Error details:", errorText);
Deno.exit(2);
}
// Grab the raw SVG text from your Vercel app
const svgData = await response.text();
// Create the directory if it doesn't exist
const dirMatch = outputPath.match(/(.*)\/[^/]+$/);
if (dirMatch) {
const dir = dirMatch[1];
try {
await Deno.mkdir(dir, { recursive: true });
} catch {
console.error("Failed to create directory. No permission?");
Deno.exit(3);
}
}
// Save the SVG to the repository
await Deno.writeTextFile(outputPath, svgData);
console.log(`Successfully wrote SVG to ${outputPath}`);
} catch (error) {
console.error("Network or execution error while fetching:", error);
Deno.exit(4);
}
}
await main();