refactor: massive USRPFP overhaul (#3)

This repo completely overhauls how USRPFP works. Some changes include:
- Deleted some leftover files
- Edited README.md a bit
- Avatars and badges are now stored in `db/data.json`
- CSS file (`db/dist.css`) is automatically generated by a GitHub
workflow

If you have questions, lmk
This commit is contained in:
nexpid 2023-08-29 12:12:01 +02:00 committed by GitHub
parent ea2d738175
commit 5a5d2c2a2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 288 additions and 377 deletions

39
.github/scripts/convert/index.mjs vendored Normal file
View file

@ -0,0 +1,39 @@
import { readFile, writeFile } from "fs/promises";
import { join } from "path";
console.time("Done");
console.log("Reading avatarsdatabase.css...");
const avis = (await readFile(join("../../", "avatarsdatabase.css"), "utf8"))
.replace(/\r/g, "")
.split("\n");
const imageMatcher = /url\((?:'|")([^'"]+)(?:'|")\)/;
const avatarMatcher = /^\/\* Custom avatar for ([0-9]+) \*\/$/;
const badgeMatcher = /^\/\* Custom badge for (.*?) \*\/$/;
const avatars = {};
for (let i = 0; i < avis.length; i++) {
const l = avis[i];
const id = l.match(avatarMatcher)?.[1];
if (id) avatars[id] = avis[i + 2].match(imageMatcher)[1];
}
const badges = {};
for (let i = 0; i < avis.length; i++) {
const l = avis[i];
const username = l.match(badgeMatcher)?.[1];
if (username) badges[username] = avis[i + 10].match(imageMatcher)[1];
}
await writeFile(
join("../../", "db", "data.json"),
JSON.stringify(
{
avatars,
badges,
},
undefined,
4
)
);

55
.github/scripts/generate/index.mjs vendored Normal file
View file

@ -0,0 +1,55 @@
import { readFile, writeFile } from "fs/promises";
import { join } from "path";
import UglifyCSS from "uglifycss";
console.time("Done");
const uglify = !process.argv.includes("--debug");
console.log("Getting templates...");
const templateLines = (
await readFile(join("../../", "db", "template.css"), "utf8")
)
.replace(/\r/g, "")
.split("\n");
const templates = {
avatar: "",
badge: "",
};
for (const id of Object.keys(templates)) {
const start = templateLines.findIndex(
(x) => x === `/* ${id.toUpperCase()}-TEMPLATE-BEGIN */`
),
end = templateLines.findIndex(
(x) => x === `/* ${id.toUpperCase()}-TEMPLATE-END */`
);
if (start >= 0 && end >= 0)
templates[id] = templateLines.slice(start + 1, end).join("\n");
else throw new Error(`Failed to get template lines for: ${id}`);
}
console.log("Generating dist.css...");
const data = JSON.parse(
await readFile(join("../../", "db", "data.json"), "utf8")
);
const dist = [];
for (const [id, img] of Object.entries(data.avatars)) {
dist.push(templates.avatar.replace(/{id}/g, id).replace(/{img}/g, img));
}
for (const [username, img] of Object.entries(data.badges)) {
dist.push(
templates.badge.replace(/{username}/g, username).replace(/{img}/g, img)
);
}
await writeFile(
join("../../", "db", "dist.css"),
uglify ? UglifyCSS.processString(dist.join("\n")) : dist.join("\n\n")
);
console.timeEnd("Done");

12
.github/scripts/package.json vendored Normal file
View file

@ -0,0 +1,12 @@
{
"name": "scripts",
"private": true,
"scripts": {
"convert": "node convert/index.mjs",
"generate": "node generate/index.mjs",
"generate:debug": "node generate/index.mjs --debug"
},
"dependencies": {
"uglifycss": "^0.0.29"
}
}

18
.github/scripts/pnpm-lock.yaml generated vendored Normal file
View file

@ -0,0 +1,18 @@
lockfileVersion: '6.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
dependencies:
uglifycss:
specifier: ^0.0.29
version: 0.0.29
packages:
/uglifycss@0.0.29:
resolution: {integrity: sha512-J2SQ2QLjiknNGbNdScaNZsXgmMGI0kYNrXaDlr4obnPW9ni1jljb1NeEVWAiTgZ8z+EBWP2ozfT9vpy03rjlMQ==}
engines: {node: '>=6.4.0'}
hasBin: true
dev: false