2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-25 01:52:03 +00:00
iconify/packages/core/src/modern/index.ts

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-12-09 21:12:00 +00:00
import { promises as fs } from 'fs'
import type { IconifyJSON } from '@iconify/types'
import type { FullIconifyIcon } from '@iconify/utils'
import { defaultCustomisations as DefaultIconCustomizations, iconToSVG, getIconData, tryInstallPkg } from '@iconify/utils';
import createDebugger from 'debug'
import { isPackageExists, resolveModule } from 'local-pkg'
const debug = createDebugger('@iconify-core:icon')
const debugModern = createDebugger('@iconify-core:modern')
const debugLegacy = createDebugger('@iconify-core:legacy')
export interface ResolvedIconPath {
collection: string
icon: string
query: Record<string, string | undefined>
}
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {}
const isLegacyExists = isPackageExists('@iconify/json')
export async function loadCollection(name: string, autoInstall = false): Promise<IconifyJSON | undefined> {
if (!_collections[name])
_collections[name] = task()
return _collections[name]
async function task(): Promise<IconifyJSON | undefined> {
let jsonPath = resolveModule(`@iconify-json/${name}/icons.json`)
if (jsonPath)
debugModern(name)
if (!jsonPath && isLegacyExists) {
jsonPath = resolveModule(`@iconify/json/json/${name}.json`)
if (jsonPath)
debugLegacy(name)
}
if (!jsonPath && !isLegacyExists && autoInstall) {
await tryInstallPkg(`@iconify-json/${name}`)
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`)
}
if (jsonPath) {
return JSON.parse(await fs.readFile(jsonPath, 'utf8'))
}
else {
debugModern(`failed to load ${name}`)
return undefined
}
}
}
export function searchForIcon(iconSet: IconifyJSON, collection: string, ids: string[], scale = 1): FullIconifyIcon | null {
let iconData: FullIconifyIcon | null
for (const id of ids) {
iconData = getIconData(iconSet, id, true)
if (iconData) {
debug(`${collection}:${id}`)
const { attributes, body } = iconToSVG(iconData, {
...DefaultIconCustomizations,
height: `${scale}em`,
width: `${scale}em`,
})
return `<svg ${Object.entries(attributes).map(i => `${i[0]}="${i[1]}"`).join(' ')}>${body}</svg>`
}
}
return null
}