import fs from "fs-extra" ; import path from "path" ; import color from "color" ; import sharp from "sharp" ; import { Item } from "alfred-hugo" ; import { IconsRebuildOptions , Octicon } from "./types" ; export class Icons { public static all ( ) { const octicons = fs . readJsonSync ( require . resolve ( "@primer/octicons/build/data.json" ) ) ; return octicons ; } /**
     * Get list of used icons in projects
     */ public static usedIcons ( items ) { const icons = { } ; const octicons = this . all ( ) ; const iconPath = path . join ( __dirname , "icons" ) ; for ( const item of items ) { if ( ! item . icon ) { continue ; } const icon = path . parse ( item . icon . path ) ; if ( icon . dir === iconPath && octicons [ icon . name ] !== undefined ) { icons [ icon . name ] = octicons [ icon . name ] ; } } return icons ; } /**
     * Rebuild icon(s)
     */ public static async rebuild ( items , options = { } ) { const iconPath = path . join ( __dirname , "icons" ) ; let iconSize = 64 ; let iconColor = color ( "#FFFFFF" ) ; if ( options . theme !== null ) { try { iconColor = color ( options . theme . result . text . color ) ; iconSize = options . theme . result . iconSize ; } catch ( e ) { } } else if ( process . env . alfred_theme_background ) { const bgColor = color ( process . env . alfred_theme_background ) ; iconColor = bgColor . grayscale ( ) . negate ( ) ; } let icons = Object . entries ( Icons . usedIcons ( items ) ) ; if ( options && options . onlyMissing === true ) { icons = icons . filter ( ( icon ) => { try { fs . statSync ( path . join ( iconPath , icon + ".png" ) ) ; return false ; } catch ( e ) { return true ; } } ) ; } if ( icons . length === 0 ) { return ; } await fs . ensureDir ( path . join ( __dirname , "icons" ) ) ; let sharpTasks ; for ( const [ name , icon ] of icons ) { const svgPath = icon . path . replace ( /\/>$/ , ` fill="${ iconColor . rgb ( ) . toString ( ) }" />` ) ; const svg = `<svg viewBox="0 0 ${ icon . width } ${ icon . height }" width="${ iconSize }" height="${ iconSize }" xmlns="http://www.w3.org/2000/svg">${ svgPath }</svg>` ; sharpTasks . push ( sharp ( Buffer . from ( svg ) ) . png ( ) . toFile ( path . join ( iconPath , `${ name }.png` ) ) ) ; } await Promise . all ( sharpTasks ) ; } }