mirror of
https://github.com/pheralb/svgl.git
synced 2025-02-06 06:58:04 +08:00
⚙️ Create check-size util for workflow.
This commit is contained in:
parent
78ad666e67
commit
a3d0277581
57
check-size/index.mjs
Normal file
57
check-size/index.mjs
Normal file
@ -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();
|
17
check-size/package.json
Normal file
17
check-size/package.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
5
check-size/pnpm-lock.yaml
generated
Normal file
5
check-size/pnpm-lock.yaml
generated
Normal file
@ -0,0 +1,5 @@
|
||||
lockfileVersion: '6.0'
|
||||
|
||||
settings:
|
||||
autoInstallPeers: true
|
||||
excludeLinksFromLockfile: false
|
Loading…
x
Reference in New Issue
Block a user