40 lines
915 B
TypeScript
40 lines
915 B
TypeScript
import { promises as fs } from "fs";
|
|
import path from "path";
|
|
import _locreq from "locreq";
|
|
|
|
const locreq = _locreq(import.meta.dirname);
|
|
|
|
let all_icon_names: Promise<string[]> | null = null;
|
|
|
|
const cache = {} as Record<string, Promise<string> | undefined>;
|
|
|
|
export const svg_icon_path = "src/icons";
|
|
|
|
async function getIconUncached(icon_name: string): Promise<string> {
|
|
return fs.readFile(
|
|
locreq.resolve(path.resolve(svg_icon_path, icon_name + ".svg")),
|
|
"utf-8"
|
|
);
|
|
}
|
|
|
|
export async function icon(icon_name: string) {
|
|
if (!cache[icon_name]) {
|
|
cache[icon_name] = getIconUncached(icon_name);
|
|
}
|
|
try {
|
|
return cache[icon_name];
|
|
} catch (e) {
|
|
console.error(e);
|
|
return "";
|
|
}
|
|
}
|
|
|
|
export async function getAllIconNames() {
|
|
if (!all_icon_names) {
|
|
all_icon_names = fs
|
|
.readdir(locreq.resolve(svg_icon_path))
|
|
.then((files) => files.map((file) => path.parse(file).name));
|
|
}
|
|
return all_icon_names;
|
|
}
|