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>2021-01-05 09:17:33 +0300
committerGitHub <noreply@github.com>2021-01-05 09:17:33 +0300
commit6e68492feb430c71019e5c8d950d38f5ab21dc5e (patch)
tree958293c949aa0aaa3257bea00d25d10cbadbe384 /build
parent7fe4d0e290248d0122773add112947da31a78f54 (diff)
Tweak build-pages.js (#636)
* switch to async/await * add a `--verbose` flag to print the existent pages, otherwise skip showing them * properly exit in case of failure * tweak success output
Diffstat (limited to 'build')
-rw-r--r--build/build-pages.js65
1 files changed, 40 insertions, 25 deletions
diff --git a/build/build-pages.js b/build/build-pages.js
index 2315184fb..afa3e7a5f 100644
--- a/build/build-pages.js
+++ b/build/build-pages.js
@@ -2,48 +2,63 @@
'use strict'
-const fs = require('fs')
+const fs = require('fs').promises
const path = require('path')
const chalk = require('chalk')
const iconsDir = path.join(__dirname, '../icons/')
const pagesDir = path.join(__dirname, '../docs/content/icons/')
+const VERBOSE = process.argv.includes('--verbose')
+
function capitalizeFirstLetter(string) {
- return string.charAt(0).toUpperCase() + string.slice(1)
+ return (string.charAt(0).toUpperCase() + string.slice(1)).split('-').join(' ')
}
-(async () => {
- try {
- const files = await fs.promises.readdir(iconsDir)
-
- for (const file of files) {
- const iconBasename = path.basename(file, path.extname(file))
- const iconTitle = capitalizeFirstLetter(iconBasename).split('-').join(' ')
- const pageName = path.join(pagesDir, `${iconBasename}.md`)
+async function main(file) {
+ const iconBasename = path.basename(file, path.extname(file))
+ const iconTitle = capitalizeFirstLetter(iconBasename)
+ const pageName = path.join(pagesDir, `${iconBasename}.md`)
- const pageTemplate = `---
+ const pageTemplate = `---
title: ${iconTitle}
categories:
tags:
---
`
- fs.access(pageName, fs.F_OK, err => {
- if (err) {
- fs.writeFile(pageName, pageTemplate, err => {
- if (err) {
- throw err
- }
-
- console.log(chalk.green(`${iconBasename} successfully created`))
- })
- } else {
- console.log(chalk.cyan(`${iconBasename}: Permalink already exists`))
- }
- })
+ try {
+ await fs.access(pageName, fs.F_OK)
+
+ if (VERBOSE) {
+ console.log(`${chalk.cyan(iconBasename)}: Page already exists; skipping`)
}
+ } catch (_) {
+ await fs.writeFile(pageName, pageTemplate)
+ console.log(chalk.green(`${iconBasename}: Page created`))
+ }
+}
+
+(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)
+
+ await Promise.all(files.map(file => {
+ return main(file).catch(error => Promise.reject(error))
+ }))
+
+ const filesLength = files.length
+
+ console.log(chalk.green('\nSuccess, %s page%s prepared!'), filesLength, filesLength !== 1 ? 's' : '')
+ console.timeEnd(timeLabel)
} catch (error) {
- console.error(chalk.cyan(`Error: ${error}`))
+ console.error(error)
+ process.exit(1)
}
})()