From e0260fcbdac364f2de87121fd092384a4514247b Mon Sep 17 00:00:00 2001 From: Eric Jinks Date: Mon, 21 May 2018 10:56:52 +1000 Subject: [PATCH] Use ora for build console message --- CHANGELOG.md | 4 ++++ functions/parse-content.js | 11 ++++++++--- functions/resize-images.js | 30 +++++++++++++++++++++--------- package.json | 1 + yarn.lock | 31 +++++++++++++++++++++++++++++-- 5 files changed, 63 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17a5bd2..6bf74f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.2 - 2018-05-21 + +* Use ora for build console message + ## 0.2.1 - 2018-05-17 * Log netlifySiteUrl when viewing CMS on localhost diff --git a/functions/parse-content.js b/functions/parse-content.js index 16290f6..864668d 100644 --- a/functions/parse-content.js +++ b/functions/parse-content.js @@ -5,6 +5,7 @@ const _mergeWith = require('lodash/mergeWith') const _isArray = require('lodash/isArray') const globCb = require('glob') const util = require('util') +const ora = require('ora') const glob = util.promisify(globCb) const readFile = util.promisify(fs.readFile) @@ -16,6 +17,8 @@ const options = { outputFile: './src/data.json' } +let spinner + const getCollectionType = filePath => { const pathParsed = path.parse(filePath) const objectKey = pathParsed.dir @@ -48,6 +51,7 @@ const parseYaml = data => { const getFileContents = filePath => { return readFile(filePath, 'utf8').then(data => { + spinner.start(`Processing ${filePath}`) if (getDocumentExt(filePath) === '.md') { data = parseMarkdown(data) } @@ -59,7 +63,7 @@ const getFileContents = filePath => { documentData.body = documentData.body || documentData.content let obj = {} _set(obj, getCollectionType(filePath), [documentData]) - console.log(`✨ Processed ${filePath}`) + spinner.succeed(`Processed ${filePath}`) return obj }) } @@ -70,7 +74,8 @@ const combineJSON = async () => { // mergeCustomiser concats arrays items const mergeCustomiser = (objValue, srcValue) => _isArray(objValue) ? objValue.concat(srcValue) : objValue - console.log(`✨ Reading JSON files in ${options.contentDir}`) + + spinner = ora(`Reading JSON files in ${options.contentDir}`).start() const paths = await glob(`${options.contentDir}/**/**.+(json|md|yaml|yml)`) const results = await readFiles(paths) const data = _mergeWith({}, ...results, mergeCustomiser) @@ -80,7 +85,7 @@ const combineJSON = async () => { const writeJSON = async () => { const json = await combineJSON() fs.writeFileSync(options.outputFile, json) - console.log(`✅ Data saved to ${options.outputFile}`) + spinner.succeed(`Data saved to ${options.outputFile}`) process.exit() } diff --git a/functions/resize-images.js b/functions/resize-images.js index dd22566..01b8732 100644 --- a/functions/resize-images.js +++ b/functions/resize-images.js @@ -3,6 +3,7 @@ const path = require('path') const globCb = require('glob') const util = require('util') const sharp = require('sharp') +const ora = require('ora') const glob = util.promisify(globCb) const readFile = util.promisify(fs.readFile) @@ -17,23 +18,28 @@ const options = { imageFormats: ['jpg', 'jpeg', 'png', 'gif', 'webp'] } -const saveImage = ({ buffer, size, outputFile }) => { +let spinner + +const saveImage = ({ buffer, size, outputFile, spinner }) => { return new Promise((resolve, reject) => { sharp(buffer) .resize(size) .withoutEnlargement() .toFile(outputFile, err => { if (err) { + spinner.fail(err) return reject(err) } else { - return resolve(console.log(`✅ Saved ${outputFile}`)) + spinner.succeed(`Saved ${outputFile}`) + return resolve(`Saved ${outputFile}`) } }) }) } const saveImages = ({ buffer, filename }) => { - console.log(`🎞 Processing ${filename}`) + spinner.text = `Processing ${filename}` + return Promise.all( options.sizes.map(async size => { const extname = path.extname(filename) @@ -43,8 +49,11 @@ const saveImages = ({ buffer, filename }) => { )}.${size}${extname}` const outputFile = path.resolve(options.outputDir, newFilename) const fileExists = await doesFileExist({ filename: outputFile }) - if (fileExists) return console.log(`â†Šī¸ ${outputFile} exists, skipping`) - return saveImage({ buffer, size, outputFile }) + if (fileExists) { + spinner.info(`${outputFile} exists, skipping`) + return `${outputFile} exists, skipping` + } + return saveImage({ buffer, size, outputFile, spinner }) }) ) } @@ -67,7 +76,7 @@ const doesFileExist = async ({ filename }) => { } const resizeImages = async () => { - console.log(`✨ Reading image files in ${options.inputDir}`) + spinner = ora(`Reading image files in ${options.inputDir}`).start() try { const fileGlob = `${options.inputDir}/**/**.+(${options.imageFormats.join( '|' @@ -79,10 +88,13 @@ const resizeImages = async () => { const filesToResize = files.filter(filename => !filename.match(ignore)) const imageFiles = await readFiles(filesToResize) Promise.all(imageFiles.map(saveImages)) - .then(() => process.exit()) - .catch(console.error) + .then(() => { + spinner.succeed(`Finished reading image files in ${options.inputDir}`) + process.exit() + }) + .catch(spinner.fail) } catch (e) { - console.log(e) + spinner.fail(e) process.exit(1) } } diff --git a/package.json b/package.json index 1bf9da4..f613976 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "heml": "^1.1.3", "js-yaml": "^3.10.0", "npm-run-all": "^4.1.2", + "ora": "^2.1.0", "postcss": "^6.0.14", "postcss-cli": "^5.0.0", "postcss-cssnext": "^3.0.2", diff --git a/yarn.lock b/yarn.lock index f6e6d67..ce4fde6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1656,7 +1656,7 @@ chalk@1.1.3, chalk@^1.0.0, chalk@^1.1.1, chalk@^1.1.3: strip-ansi "^3.0.0" supports-color "^2.0.0" -chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.4.1: +chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.0, chalk@^2.3.1, chalk@^2.4.1: version "2.4.1" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.1.tgz#18c49ab16a037b6eb0152cc83e3471338215b66e" dependencies: @@ -1821,6 +1821,10 @@ cli-cursor@^2.0.0, cli-cursor@^2.1.0: dependencies: restore-cursor "^2.0.0" +cli-spinners@^1.1.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-1.3.1.tgz#002c1990912d0d59580c93bd36c056de99e4259a" + cli-width@^2.0.0: version "2.2.0" resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639" @@ -2444,6 +2448,12 @@ default-require-extensions@^1.0.0: dependencies: strip-bom "^2.0.0" +defaults@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/defaults/-/defaults-1.0.3.tgz#c656051e9817d9ff08ed881477f3fe4019f3ef7d" + dependencies: + clone "^1.0.2" + define-properties@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.2.tgz#83a73f2fea569898fb737193c8f873caf6d45c94" @@ -5463,7 +5473,7 @@ log-symbols@^1.0.2: dependencies: chalk "^1.0.0" -log-symbols@^2.0.0: +log-symbols@^2.0.0, log-symbols@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a" dependencies: @@ -6169,6 +6179,17 @@ optionator@^0.8.1, optionator@^0.8.2: type-check "~0.3.2" wordwrap "~1.0.0" +ora@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/ora/-/ora-2.1.0.tgz#6caf2830eb924941861ec53a173799e008b51e5b" + dependencies: + chalk "^2.3.1" + cli-cursor "^2.1.0" + cli-spinners "^1.1.0" + log-symbols "^2.2.0" + strip-ansi "^4.0.0" + wcwidth "^1.0.1" + original@>=0.0.5: version "1.0.1" resolved "https://registry.yarnpkg.com/original/-/original-1.0.1.tgz#b0a53ff42ba997a8c9cd1fb5daaeb42b9d693190" @@ -9252,6 +9273,12 @@ wbuf@^1.1.0, wbuf@^1.7.2: dependencies: minimalistic-assert "^1.0.0" +wcwidth@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8" + dependencies: + defaults "^1.0.3" + web-resource-inliner@^4.2.0: version "4.2.1" resolved "https://registry.yarnpkg.com/web-resource-inliner/-/web-resource-inliner-4.2.1.tgz#a3ec33d85794675cb526cfb93d2115741869f8be"