mirror of
https://github.com/iconify/iconify.git
synced 2024-12-12 21:57:50 +00:00
Check if file exists asynchronously in FileSystemIconLoader
This commit is contained in:
parent
24ab828457
commit
3d1a89ef77
@ -1,8 +1,11 @@
|
|||||||
import type { Awaitable } from '@antfu/utils';
|
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 type { CustomIconLoader } from './types';
|
||||||
import { camelize, pascalize } from '../misc/strings';
|
import { camelize, pascalize } from '../misc/strings';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns CustomIconLoader for loading icons from a directory
|
||||||
|
*/
|
||||||
export function FileSystemIconLoader(
|
export function FileSystemIconLoader(
|
||||||
dir: string,
|
dir: string,
|
||||||
transform?: (svg: string) => Awaitable<string>
|
transform?: (svg: string) => Awaitable<string>
|
||||||
@ -14,7 +17,13 @@ export function FileSystemIconLoader(
|
|||||||
`${dir}/${pascalize(name)}.svg`,
|
`${dir}/${pascalize(name)}.svg`,
|
||||||
];
|
];
|
||||||
for (const path of paths) {
|
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');
|
const svg = await fs.readFile(path, 'utf-8');
|
||||||
return typeof transform === 'function'
|
return typeof transform === 'function'
|
||||||
? await transform(svg)
|
? await transform(svg)
|
||||||
|
Loading…
Reference in New Issue
Block a user