diff options
author | Johann-S <johann.servoire@gmail.com> | 2019-09-16 12:50:50 +0300 |
---|---|---|
committer | XhmikosR <xhmikosr@gmail.com> | 2019-09-16 13:17:03 +0300 |
commit | cfb433e79f7651e71bff57835fd7dd661e22d889 (patch) | |
tree | 55f1390a11ae3f327c013588b92a0c73aca51245 /build | |
parent | b7b8ccdf705d320a8e64e939ca4709fbd95ce870 (diff) |
improve build svgs script
Diffstat (limited to 'build')
-rw-r--r-- | build/build-svgs.js | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/build/build-svgs.js b/build/build-svgs.js index 6a7d8adf5..8fc1f41e0 100644 --- a/build/build-svgs.js +++ b/build/build-svgs.js @@ -1,10 +1,17 @@ +#!/usr/bin/env node + 'use strict' +const { promisify } = require('util') const fs = require('fs') const path = require('path') const chalk = require('chalk') const cheerio = require('cheerio') +const pReaddir = promisify(fs.readdir) +const pReadFile = promisify(fs.readFile) +const pWriteFile = promisify(fs.writeFile) + const iconsDir = path.join(__dirname, '../icons/') const svgAttributes = { @@ -16,19 +23,11 @@ const svgAttributes = { xmlns: 'http://www.w3.org/2000/svg' } -fs.readdir(iconsDir, (error, files) => { - if (error) { - throw error - } - - files.forEach(file => { - file = path.join(iconsDir, file) - - fs.readFile(file, 'utf8', (err, data) => { - if (err) { - throw err - } +const processFile = file => new Promise((resolve, reject) => { + file = path.join(iconsDir, file) + pReadFile(file, 'utf8') + .then(data => { const $ = cheerio.load(data) const svg = $('svg') @@ -41,18 +40,22 @@ fs.readdir(iconsDir, (error, files) => { $(svg).attr('class', `bi bi-${path.basename(file, '.svg')}`) - fs.writeFile(file, $(svg), 'utf8', err => { - if (err) { - throw err - } - - console.log(`- ${path.basename(file, '.svg')}`) - }) + pWriteFile(file, $(svg), 'utf8') + .then(() => { + console.log(`- ${path.basename(file, '.svg')}`) + resolve() + }) + .catch(err => reject(err)) }) - }) - - // the setTimeout should be removed - setTimeout(() => { - console.log(chalk.green(`\nSuccess, ${files.length} icons prepped!`)) - }, 1000) + .catch(err => reject(err)) }) + +const main = async () => { + const files = await pReaddir(iconsDir) + + await Promise.all(files.map(file => processFile(file))) + + console.log(chalk.green(`\nSuccess, ${files.length} icons prepped!`)) +} + +main() |