From 4bc1f1669d6870dba474bec072db16571c7a4ed8 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Tue, 29 Dec 2020 22:25:37 +0200 Subject: build-svgs.js: switch to async/await and tweaks (#603) * build-svgs.js: switch to async/await * build-svgs.js: return early If the optimized SVG is identical to the original SVG, skip writing the file to the disk. --- build/build-svgs.js | 118 +++++++++++++++++++++++----------------------------- 1 file changed, 53 insertions(+), 65 deletions(-) (limited to 'build') diff --git a/build/build-svgs.js b/build/build-svgs.js index 766ff408f..8c282343e 100644 --- a/build/build-svgs.js +++ b/build/build-svgs.js @@ -23,79 +23,67 @@ const svgAttributes = { } const getSvgoConfig = async () => { - try { - let svgoConfig = await fs.readFile(path.join(__dirname, '../svgo.yml'), 'utf8') - - svgoConfig = await yaml.safeLoad(svgoConfig) + const svgoConfigFile = await fs.readFile(path.join(__dirname, '../svgo.yml'), 'utf8') - return svgoConfig - } catch (error) { - console.error('Couldn\'t read SVGO\'s config!') - console.error(error) - process.exit(1) - } + return await yaml.safeLoad(svgoConfigFile) } -const processFile = (file, config) => { - return new Promise((resolve, reject) => { - const filepath = path.join(iconsDir, file) - const basename = path.basename(file, '.svg') - - fs.readFile(filepath, 'utf8') - .then(data => { - const svgo = new SVGO(config) - - svgo.optimize(data) - .then(result => { - const $ = cheerio.load(result.data, { - xml: { - xmlMode: true - } - }) - const $svgElement = $('svg') - - // We keep all SVG contents apart from the `` element. - // `$(this)` refers to the original object not the replaced one! - $svgElement.replaceWith($('').append($(this).html())) - - // Then we set the `svgAttributes` in the order we want to, - // hence why we remove the attributes and add them back - for (const [attribute, value] of Object.entries(svgAttributes)) { - $svgElement.removeAttr(attribute) - $svgElement.attr(attribute, attribute === 'class' ? `bi bi-${basename}` : value) - } - - fs.writeFile(filepath, $svgElement.toString().replace(/\r\n?/g, '\n'), 'utf8') - .then(() => { - if (VERBOSE) { - console.log(`- ${basename}`) - } - resolve() - }) - .catch(error => reject(error)) - }) - .catch(error => reject(error)) - }) - .catch(error => reject(error)) - }) -} +const processFile = async (file, config) => { + const filepath = path.join(iconsDir, file) + const basename = path.basename(file, '.svg') + + const originalSvg = await fs.readFile(filepath, 'utf8') + const svgo = await new SVGO(config) + const optimizedSvg = await svgo.optimize(originalSvg) -const main = async () => { - const basename = path.basename(__filename) - const timeLabel = chalk.cyan(`[${basename}] finished`) + const $ = cheerio.load(optimizedSvg.data, { + xml: { + xmlMode: true + } + }) + const $svgElement = $('svg') - console.log(chalk.cyan(`[${basename}] started`)) - console.time(timeLabel) + // We keep all SVG contents apart from the `` element. + // `$(this)` refers to the original object not the replaced one! + $svgElement.replaceWith($('').append($(this).html())) - const files = await fs.readdir(iconsDir) - const config = await getSvgoConfig() + // Then we set the `svgAttributes` in the order we want to, + // hence why we remove the attributes and add them back + for (const [attribute, value] of Object.entries(svgAttributes)) { + $svgElement.removeAttr(attribute) + $svgElement.attr(attribute, attribute === 'class' ? `bi bi-${basename}` : value) + } - await Promise.all(files.map(file => processFile(file, config))) + const resultSvg = $svgElement.toString().replace(/\r\n?/g, '\n') - const filesLength = files.length + if (resultSvg !== originalSvg) { + await fs.writeFile(filepath, resultSvg, 'utf8') + } - console.log(chalk.green(`\nSuccess, ${filesLength} icon${filesLength !== 1 ? 's' : ''} prepped!`)) - console.timeEnd(timeLabel) + if (VERBOSE) { + console.log(`- ${basename}`) + } } -main() +(async () => { + try { + const basename = path.basename(__filename) + const timeLabel = chalk.cyan(`[${basename}] finished`) + + console.log(chalk.cyan(`[${basename}] started`)) + console.time(timeLabel) + + const files = await fs.readdir(iconsDir) + const config = await getSvgoConfig() + + await Promise.all(files.map(file => processFile(file, config))) + + const filesLength = files.length + + console.log(chalk.green(`\nSuccess, ${filesLength} icon${filesLength !== 1 ? 's' : ''} prepped!`)) + console.timeEnd(timeLabel) + } catch (error) { + console.error(error) + process.exit(1) + } +})() -- cgit v1.2.3