From b64751fe84e9a41bf598a85f5c31d72fb2ef7fea Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 3 Apr 2023 17:32:31 +0200 Subject: [PATCH] chore: refactor checkFileSize Close #1755 --- scripts/check-size.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/scripts/check-size.js b/scripts/check-size.js index f629ea5f..2c95c4f6 100644 --- a/scripts/check-size.js +++ b/scripts/check-size.js @@ -1,18 +1,23 @@ -const fs = require('fs') +const fs = require('fs').promises const path = require('path') const chalk = require('chalk') const { gzipSync } = require('zlib') const { compress } = require('brotli') -function checkFileSize(filePath) { - if (!fs.existsSync(filePath)) { +async function checkFileSize(filePath) { + const stat = await fs.stat(filePath).catch(() => null) + if (!stat?.isFile()) { + console.error(chalk.red(`File ${chalk.bold(filePath)} not found`)) return } - const file = fs.readFileSync(filePath) + const file = await fs.readFile(filePath) const minSize = (file.length / 1024).toFixed(2) + 'kb' - const gzipped = gzipSync(file) + const [gzipped, compressed] = await Promise.all([ + gzipSync(file), + // + compress(file), + ]) const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb' - const compressed = compress(file) const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb' console.log( `${chalk.gray( @@ -21,9 +26,15 @@ function checkFileSize(filePath) { ) } -checkFileSize( - path.resolve(__dirname, '../packages/router/size-checks/dist/webRouter.js') -) -checkFileSize( - path.resolve(__dirname, '../packages/router/dist/vue-router.global.prod.js') -) +;(async () => { + const files = [ + path.resolve(__dirname, '../packages/router/size-checks/dist/webRouter.js'), + path.resolve( + __dirname, + '../packages/router/dist/vue-router.global.prod.js' + ), + ] + for (const file of files) { + await checkFileSize(file) + } +})() -- 2.39.5