2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +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 5d6885369f
commit 3f90e54129

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)