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[] = []; for await (const 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(resolve => { svg2img( svgBuffer, { format: "png", resvg: { fitTo: { mode: "width", value: 128 } } }, function (_err, buffer) { resolve(buffer); } ) } ) await this.iconCache.set(iconName, pngBuffer); } }