import { favicons } from 'favicons'
import fs from 'fs'

const source = 'src/assets/logo/logo.png'

const cfg = {
    path: 'assets/logo/favicon',
    appName: 'Garrett Mills',
    appDescription: '',
    background: '#111111',
    theme_color: '#111111',
}

// Generate the favicons and metadata:
const res = await favicons(source, cfg)

// Write out the favicon images:
for ( const img of res.images ) {
    fs.writeFileSync(`src/assets/logo/favicon/${img.name}`, img.contents)

    if ( img.name === 'favicon.ico' ) {
        // Write the favicon to the root to support older browsers
        fs.writeFileSync('src/favicon.ico', img.contents)
    }
}

// Filter out the WebManifest & friends from the HTML, since we're not using it currently:
const fileNames = res.files.map(x => x.name)
const htmlLines = res.html.filter(line =>
    fileNames.every(name => !line.includes(name)))

// Write the favicon metadata to a file to be picked up by Eleventy
fs.writeFileSync('src/_includes/meta.html', htmlLines.join('\n'))
