2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-12 13:47:49 +00:00

Check if file exists asynchronously in FileSystemIconLoader

This commit is contained in:
Vjacheslav Trushkin 2022-01-09 22:41:43 +02:00
parent 24ab828457
commit 3d1a89ef77

View File

@ -1,8 +1,11 @@
import type { Awaitable } from '@antfu/utils';
import { existsSync, promises as fs } from 'fs';
import { promises as fs, Stats } from 'fs';
import type { CustomIconLoader } from './types';
import { camelize, pascalize } from '../misc/strings';
/**
* Returns CustomIconLoader for loading icons from a directory
*/
export function FileSystemIconLoader(
dir: string,
transform?: (svg: string) => Awaitable<string>
@ -14,7 +17,13 @@ export function FileSystemIconLoader(
`${dir}/${pascalize(name)}.svg`,
];
for (const path of paths) {
if (existsSync(path)) {
let stat: Stats;
try {
stat = await fs.lstat(path);
} catch (err) {
continue;
}
if (stat.isFile()) {
const svg = await fs.readFile(path, 'utf-8');
return typeof transform === 'function'
? await transform(svg)