-
Notifications
You must be signed in to change notification settings - Fork 2
/
generateAssets.js
156 lines (140 loc) · 5.14 KB
/
generateAssets.js
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
146
147
148
149
150
151
152
153
154
155
156
const { Octokit } = require("@octokit/rest");
const fs = require("fs");
const { createTokenAuth } = require("@octokit/auth-token");
const sharp = require("sharp");
const fullLogo = {
light: "https://raw.githubusercontent.com/Draco-lang/Language-suggestions/main/Resources/Logo-Long.svg",
dark: "https://raw.githubusercontent.com/Draco-lang/Language-suggestions/main/Resources/Logo-Long-Inverted.svg",
};
const shortLogo = {
light: "https://raw.githubusercontent.com/Draco-lang/Language-suggestions/main/Resources/Logo-Short.svg",
dark: "https://raw.githubusercontent.com/Draco-lang/Language-suggestions/main/Resources/Logo-Short-Inverted.svg",
};
const githubLogo = {
light: "prebuild_assets/github-mark.svg",
dark: "prebuild_assets/github-mark-white.svg",
};
const emojis = [];
async function main() {
if (!fs.existsSync("public/generated")) {
fs.mkdirSync("public/generated", { recursive: true });
}
await downloadThemedImage(fullLogo, "public/generated/Logo-Long.svg", true);
await downloadThemedImage(shortLogo, "public/generated/Logo-Short.svg", true);
await downloadThemedImage(githubLogo, "public/generated/github-logo.svg", false);
await downloadAndConvertSvgToPng(
"https://raw.githubusercontent.com/Draco-lang/Language-suggestions/main/Resources/Logo-Short-Inverted-Outline.svg",
"public/generated/Logo-Short-Inverted-Outline.png"
);
await download(
"https://raw.githubusercontent.com/Draco-lang/Language-suggestions/main/Resources/Derpy-Outlined.svg",
"public/generated/derpy.svg"
);
emojis.push("derpy");
let octokit;
if (process.env.GITHUB_TOKEN !== undefined && process.env.GITHUB_TOKEN.length > 0) {
const auth = createTokenAuth(process.env.GITHUB_TOKEN);
const authentication = await auth();
octokit = new Octokit({
auth: authentication.token,
});
} else {
octokit = new Octokit();
}
const response = await octokit.repos.getContent({
owner: "Draco-lang",
repo: "Language-suggestions",
path: "Resources/Emojis",
});
const promises = response.data.map(async (element) => {
console.log(`Downloading ${element.name}...`);
const resp = await fetch(element.download_url);
const emoji = await resp.text();
await fs.promises.writeFile(`public/generated/${element.name}`, emoji);
});
await Promise.all(promises);
response.data.map((s) => s.name.replace(/\.[^/.]+$/, "")).forEach((s) => emojis.push(s));
await fs.promises.writeFile(
"src/generated/emojiTypes.ts",
`export type EmojiName = ${emojis.map((s) => `"${s}"`).join(" | ")};`
);
// copy hljs themes to public folder
await fs.promises.copyFile(
"node_modules/highlight.js/styles/atom-one-light.css",
"public/generated/atom-one-light.css"
);
await fs.promises.copyFile(
"node_modules/highlight.js/styles/atom-one-dark.css",
"public/generated/atom-one-dark.css"
);
}
main();
async function download(url, path) {
const resp = await fetch(url);
const text = await resp.text();
await fs.promises.writeFile(path, text);
}
async function downloadThemedImage(urls, outputPath, isUrl) {
const svg = await createThemeBasedLogo(urls.light, urls.dark, isUrl);
await fs.promises.writeFile(outputPath, svg);
console.log(`Image downloaded and saved as ${outputPath}`);
}
function stripXMLHeader(xml) {
const regex = /<\??xml[^>]*>/;
return xml.replace(regex, "");
}
function stripViewBox(xml) {
const regex = /viewBox="[^"]*"/;
return xml.replace(regex, "");
}
function extractOpeningSvgTag(svgContent) {
const openingSvgTagRegex = /<svg\b[^>]*>/;
const openingSvgTagMatch = svgContent.match(openingSvgTagRegex);
return openingSvgTagMatch ? openingSvgTagMatch[0] : '<svg xmlns="http://www.w3.org/2000/svg">';
}
async function createThemeBasedLogo(lightUrl, darkUrl, isUrl) {
let bodyLight, bodyDark;
if (isUrl) {
const responseLight = await fetch(lightUrl);
const responseDark = await fetch(darkUrl);
bodyLight = await responseLight.text();
bodyDark = await responseDark.text();
} else {
bodyLight = await fs.promises.readFile(lightUrl, "utf-8");
bodyDark = await fs.promises.readFile(darkUrl, "utf-8");
}
let logoLight = stripXMLHeader(bodyLight);
logoLight = stripViewBox(logoLight);
let logoDark = stripXMLHeader(bodyDark);
logoDark = stripViewBox(logoDark);
const openingSvgTag = extractOpeningSvgTag(bodyLight);
const logoSvg = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
${openingSvgTag}
<style>
@media (prefers-color-scheme: dark) {
.light{
visibility: hidden;
}
}
@media (prefers-color-scheme: light) {
.dark{
visibility: hidden;
}
}
</style>
<g class="dark">
${logoDark}
</g>
<g class="light">
${logoLight}
</g>
</svg>
`;
return logoSvg;
}
async function downloadAndConvertSvgToPng(url, outputPath) {
const resp = await fetch(url);
const svgContent = await resp.text();
await sharp(Buffer.from(svgContent)).png().toFile(outputPath);
console.log(`SVG converted and saved as ${outputPath}`);
}