import { installPackage } from '@antfu/install-pkg' ; import type { Awaitable } from '@antfu/utils' ; import { sleep } from '@antfu/utils' ; import { cyan , yellow } from 'kolorist' ; import type { ResolvedOptions } from '../types' ; export function camelize ( str ) { return str . replace ( /-([a-z0-9])/g , g => g [ 1 ] . toUpperCase ( ) ) ; } export function pascalize ( str ) { const camel = camelize ( str ) ; return camel [ 0 ] . toUpperCase ( ) + camel . slice ( 1 ) ; } export function camelToKebab ( key ) { const result = key . replace ( /:/g , '-' ) . replace ( /([A-Z])/g , ' $1' ) . trim ( ) ; return result . split ( /\s+/g ) . join ( '-' ) . toLowerCase ( ) ; } const warnned = new Set < string > ( ) ; export function warnOnce ( msg ) { if ( ! warnned . has ( msg ) ) { warnned . add ( msg ) ; console . warn ( yellow ( `[unplugin-icons] ${ msg }` ) ) ; } } export async function mergeIconProps ( svg , collection , icon , query , propsProvider ? , options ? ) { const props = await propsProvider ?. ( ) ?? { height : `${ options ?. scale ?? 1 }em` , width : `${ options ?. scale ?? 1 }em` , } ; await options ?. iconCustomizer ?. ( collection , icon , props ) ; Object . keys ( query ) . forEach ( ( p ) => { const v = query [ p ] ; if ( v !== undefined && v !== null ) props [ p ] = v ; } ) ; return svg . replace ( '<svg' , `<svg ${ Object . keys ( props ) . map ( p => `${ p }="${ props [ p ] }"` ) . join ( ' ' ) }` ) ; } let pending ; const tasks = { } ; export async function tryInstallPkg ( name ) { if ( pending ) await pending ; if ( ! tasks [ name ] ) { console . log ( cyan ( `Installing ${ name }...` ) ) ; tasks [ name ] = pending = installPackage ( name , { dev : true , preferOffline : true } ) . then ( ( ) => sleep ( 300 ) ) . catch ( ( e ) => { warnOnce ( `Failed to install ${ name }` ) ; console . error ( e ) ; } ) . finally ( ( ) => { pending = undefined ; } ) ; } return tasks [ name ] ! ; }