Skip to content

Commit

Permalink
feat: add transform callback to FileSystemIconLoader (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored Nov 30, 2021
1 parent 7509caa commit 715ca6c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/loaders.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { existsSync, promises as fs } from 'fs'
import { Awaitable } from '@antfu/utils'
import { camelize, pascalize } from './core/utils'
import { CustomIconLoader } from '.'

export function FileSystemIconLoader(dir: string): CustomIconLoader {
return (name) => {
export function FileSystemIconLoader(dir: string, transform?: (svg: string) => Awaitable<string>): CustomIconLoader {
return async(name) => {
const pathes = [
`${dir}/${name}.svg`,
`${dir}/${camelize(name)}.svg`,
`${dir}/${pascalize(name)}.svg`,
]
for (const path of pathes) {
if (existsSync(path))
return fs.readFile(path, 'utf-8')
if (existsSync(path)) {
const svg = await fs.readFile(path, 'utf-8')
return typeof transform === 'function' ? await transform(svg) : svg
}
}
}
}

0 comments on commit 715ca6c

Please sign in to comment.