Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/icons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
authorXhmikosR <xhmikosr@gmail.com>2020-12-29 23:25:37 +0300
committerGitHub <noreply@github.com>2020-12-29 23:25:37 +0300
commit4bc1f1669d6870dba474bec072db16571c7a4ed8 (patch)
treec15f062fc9866f291990cdfeb7fd77b581a5ad68 /build
parenta7b4834b8abc8105bdad3f7c56c5369f5d35cdb2 (diff)
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.
Diffstat (limited to 'build')
-rw-r--r--build/build-svgs.js118
1 files changed, 53 insertions, 65 deletions
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 `<svg>` element.
- // `$(this)` refers to the original object not the replaced one!
- $svgElement.replaceWith($('<svg>').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 `<svg>` element.
+ // `$(this)` refers to the original object not the replaced one!
+ $svgElement.replaceWith($('<svg>').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)
+ }
+})()