Adds deployment for icons
This commit is contained in:
parent
154002f6f3
commit
0e10ea3cab
14 changed files with 1449 additions and 53 deletions
56
source/Icons/IconDeployer.ts
Normal file
56
source/Icons/IconDeployer.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import {REST, Routes} from "discord.js";
|
||||
import path from "node:path";
|
||||
import * as fs from "node:fs";
|
||||
import svg2img from "svg2img";
|
||||
import {IconCache} from "./IconCache";
|
||||
|
||||
export class IconDeployer {
|
||||
static ICON_PATH = path.resolve('public/icons')
|
||||
|
||||
constructor(
|
||||
private readonly iconCache: IconCache
|
||||
) {}
|
||||
|
||||
public async ensureExistance() {
|
||||
const directory = await fs.promises.opendir(IconDeployer.ICON_PATH);
|
||||
const addIconPromises: Promise<void>[] = [];
|
||||
for await (let dirname of directory) {
|
||||
const iconName = path.basename(dirname.name, '.svg').replaceAll('-','_');
|
||||
|
||||
if (this.iconCache.get(iconName) !== null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
addIconPromises.push(
|
||||
this.addIcon(path.resolve(dirname.parentPath, dirname.name), iconName)
|
||||
);
|
||||
}
|
||||
|
||||
await Promise.all(addIconPromises);
|
||||
}
|
||||
|
||||
private async addIcon(iconPath: string, iconName: string) {
|
||||
const svgBuffer = await fs.promises.readFile(iconPath, 'utf-8');
|
||||
const pngBuffer = await new Promise<Buffer>(resolve => {
|
||||
svg2img(
|
||||
svgBuffer,
|
||||
{
|
||||
format: "png",
|
||||
resvg: {
|
||||
fitTo: {
|
||||
mode: "width",
|
||||
value: 128
|
||||
}
|
||||
}
|
||||
},
|
||||
function (err, buffer) {
|
||||
resolve(buffer);
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
await this.iconCache.set(iconName, pngBuffer);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue