From a3d027758127ba9017ccec14ed9c78876a40b920 Mon Sep 17 00:00:00 2001 From: pheralb Date: Sat, 16 Dec 2023 20:10:46 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Create=20check-size=20util?= =?UTF-8?q?=20for=20workflow.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- check-size/index.mjs | 57 +++++++++++++++++++++++++++++++++++++++ check-size/package.json | 17 ++++++++++++ check-size/pnpm-lock.yaml | 5 ++++ 3 files changed, 79 insertions(+) create mode 100644 check-size/index.mjs create mode 100644 check-size/package.json create mode 100644 check-size/pnpm-lock.yaml diff --git a/check-size/index.mjs b/check-size/index.mjs new file mode 100644 index 0000000..054f409 --- /dev/null +++ b/check-size/index.mjs @@ -0,0 +1,57 @@ +import { readdir, stat } from 'fs/promises'; +import { join } from 'path'; + +// πŸ”Ž Settings: +const dir = '../static/library'; +const sizeLimit = 20000; // 20kb; + +function convertBytes(bytes, format = 'KB') { + if (format === 'KB') { + return (bytes / 1024).toFixed(2) + ' KB'; + } else if (format === 'MB') { + return (bytes / (1024 * 1024)).toFixed(2) + ' MB'; + } else { + return 'Formato no vΓ‘lido. Utilice "KB" o "MB".'; + } +} + +export async function checkSize() { + const files = await readdir(dir); + let maxSize = 0; + let maxFiles = []; + + try { + for (const file of files) { + const filePath = join(dir, file); + const stats = await stat(filePath); + + if (stats.size > maxSize) { + maxSize = stats.size; + maxFiles.push(file); + } + } + + if (maxFiles.length === 0) { + console.log(`- βœ… All files are smaller than ${convertBytes(sizeLimit)}`); + } else { + throw new Error( + `The following files are bigger than ${convertBytes(sizeLimit)}: ${maxFiles.join(', ')}` + ); + } + } catch (err) { + console.error(`- ❌ ${err.message}`); + } finally { + // Print the results + console.log(`- πŸ“ Directory: ${dir}`); + console.log(`- πŸ“ Size limit: ${convertBytes(sizeLimit)} bytes`); + console.log(`- πŸ“ Max size found: ${convertBytes(maxSize, 'MB')}`); + if (maxFiles.length > 0) { + maxFiles.forEach((file) => { + console.log(`- πŸ“„ File: ${file} - πŸ“¦ Size: ${convertBytes(maxSize, 'KB')}`); + }); + } + } +} + +// Run the function +checkSize(); diff --git a/check-size/package.json b/check-size/package.json new file mode 100644 index 0000000..5ae1039 --- /dev/null +++ b/check-size/package.json @@ -0,0 +1,17 @@ +{ + "name": "@svgl/check-size", + "version": "1.0.0", + "description": "Limit the size of your SVG files", + "main": "index.js", + "author": "@pheralb_", + "license": "ISC", + "keywords": [ + "svg", + "size", + "limit", + "check" + ], + "scripts": { + "start": "node index.mjs" + } +} diff --git a/check-size/pnpm-lock.yaml b/check-size/pnpm-lock.yaml new file mode 100644 index 0000000..2b9f188 --- /dev/null +++ b/check-size/pnpm-lock.yaml @@ -0,0 +1,5 @@ +lockfileVersion: '6.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false