-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (130 loc) · 4.01 KB
/
index.js
File metadata and controls
145 lines (130 loc) · 4.01 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import core from "@actions/core";
import { mkdir, writeFile } from "node:fs/promises";
import path from "node:path";
import statsApi from "github-readme-stats/api/index.js";
import repoApi from "github-readme-stats/api/pin.js";
import topLangsApi from "github-readme-stats/api/top-langs.js";
import wakatimeApi from "github-readme-stats/api/wakatime.js";
import gistApi from "github-readme-stats/api/gist.js";
/**
* Normalize option values to strings.
* @param {Record<string, unknown>} options Input options.
* @returns {Record<string, string>} Normalized options.
*/
const normalizeOptions = (options) => {
const normalized = {};
for (const [key, val] of Object.entries(options)) {
if (Array.isArray(val)) {
normalized[key] = val.join(",");
} else if (val === null || val === undefined) {
continue;
} else {
normalized[key] = String(val);
}
}
return normalized;
};
/**
* Parse options from query string or JSON and normalize values to strings.
* @param {string} value Input value.
* @returns {Record<string, string>} Parsed options.
*/
const parseOptions = (value) => {
if (!value) return {};
const trimmed = value.trim();
const options = {};
if (trimmed.startsWith("{")) {
try {
Object.assign(options, JSON.parse(trimmed));
} catch (error) {
throw new Error("Invalid JSON in options.");
}
} else {
const queryString = trimmed.startsWith("?") ? trimmed.slice(1) : trimmed;
const params = new URLSearchParams(queryString);
for (const [key, val] of params.entries()) {
if (options[key]) {
options[key] = `${options[key]},${val}`;
} else {
options[key] = val;
}
}
}
return normalizeOptions(options);
};
// Map of card types to their respective API handlers.
// TODO: Replace handler usage with a stable library API once exposed upstream.
const cardHandlers = {
stats: statsApi,
"top-langs": topLangsApi,
pin: repoApi,
wakatime: wakatimeApi,
gist: gistApi,
};
/**
* Validate required options for each card type.
* @param {string} card Card type.
* @param {Record<string, string>} query Parsed options.
* @param {string | undefined} repoOwner Repository owner from environment.
* @throws {Error} If required options are missing.
*/
const validateCardOptions = (card, query, repoOwner) => {
if (!query.username && repoOwner) {
query.username = repoOwner;
core.warning("username not provided; defaulting to repository owner.");
}
switch (card) {
case "stats":
case "top-langs":
case "wakatime":
if (!query.username) {
throw new Error(`username is required for the ${card} card.`);
}
break;
case "pin":
if (!query.repo) {
throw new Error("repo is required for the pin card.");
}
break;
case "gist":
if (!query.id) {
throw new Error("id is required for the gist card.");
}
break;
default:
break;
}
};
const run = async () => {
const card = core.getInput("card", { required: true }).toLowerCase();
const optionsInput = core.getInput("options") || "";
const outputPathInput = core.getInput("path");
const handler = cardHandlers[card];
if (!handler) {
throw new Error(`Unsupported card type: ${card}`);
}
const query = parseOptions(optionsInput);
validateCardOptions(card, query, process.env.GITHUB_REPOSITORY_OWNER);
const outputPathValue =
outputPathInput || path.join("profile", `${card}.svg`);
const outputPath = path.resolve(process.cwd(), outputPathValue);
await mkdir(path.dirname(outputPath), { recursive: true });
let svg = "";
const res = {
setHeader: () => {},
send: (value) => {
svg = value;
return value;
},
};
await handler({ query }, res);
if (!svg) {
throw new Error("Card renderer returned empty output.");
}
await writeFile(outputPath, svg, "utf8");
core.info(`Wrote ${outputPath}`);
core.setOutput("path", outputPathValue);
};
run().catch((error) => {
core.setFailed(error instanceof Error ? error.message : String(error));
});