2018-01-27 20:48:22 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
import svgs from './svgs'
|
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import pify from 'pify'
|
|
|
|
import SVGO from 'svgo'
|
|
|
|
import $ from 'cheerio'
|
2018-01-27 20:48:22 +00:00
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
const svgo = new SVGO()
|
2018-01-27 20:48:22 +00:00
|
|
|
const readFile = pify(fs.readFile.bind(fs))
|
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
export async function buildSvg () {
|
2018-01-27 20:48:22 +00:00
|
|
|
let result = (await Promise.all(svgs.map(async svg => {
|
|
|
|
let filepath = path.join(__dirname, '../', svg.src)
|
|
|
|
let content = await readFile(filepath, 'utf8')
|
|
|
|
let optimized = (await svgo.optimize(content))
|
|
|
|
let $optimized = $(optimized.data)
|
|
|
|
let $path = $optimized.find('path').removeAttr('fill')
|
|
|
|
let $symbol = $('<symbol></symbol>')
|
|
|
|
.attr('id', svg.id)
|
|
|
|
.attr('viewBox', `0 0 ${optimized.info.width} ${optimized.info.height}`)
|
|
|
|
.append($path)
|
|
|
|
return $.xml($symbol)
|
|
|
|
}))).join('\n')
|
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
return `<svg xmlns="http://www.w3.org/2000/svg" style="display:none;">\n${result}\n</svg>`
|
2018-01-27 20:48:22 +00:00
|
|
|
}
|