diff --git a/check-size/index.js b/check-size/index.js new file mode 100644 index 0000000..13b18d8 --- /dev/null +++ b/check-size/index.js @@ -0,0 +1,67 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const { readdir, stat } = require('fs').promises; +const { join } = require('path'); + +// For GitHub Actions: +const core = require('@actions/core'); + +// πŸ”Ž 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 'Invalid format. Use "KB" or "MB".'; + } +} + +async function checkSize() { + const files = await readdir(dir); + let maxSize = 0; + let maxFiles = []; + let message = ''; + + 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({ + filename: file, + size: maxSize + }); + } + } + + if (maxFiles.length === 0) { + message = `- βœ… All files are smaller than ${convertBytes(sizeLimit)}`; + console.log(message); + core.setOutput('message', message); + } else { + message = `- ❌ There are files bigger than ${convertBytes(sizeLimit)}.`; + throw new Error(message); + } + } catch (err) { + core.setFailed(message); + } finally { + if (maxFiles.length > 0) { + console.log('πŸ”Ž Files found:'); + maxFiles.forEach((file) => { + console.log(`- πŸ“„ ${file.filename} - ${convertBytes(file.size, 'KB')}`); + }); + } + console.log('βš™οΈ Settings:'); + console.log(`- πŸ“ Directory: ${dir}`); + console.log(`- 🧱 Size limit: ${convertBytes(sizeLimit)} bytes`); + console.log(`- πŸ”” Max size found: ${convertBytes(maxSize, 'MB')}`); + } +} + +// Run the function +checkSize(); diff --git a/check-size/index.mjs b/check-size/index.mjs deleted file mode 100644 index 7e28bc0..0000000 --- a/check-size/index.mjs +++ /dev/null @@ -1,51 +0,0 @@ -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 { - console.log(`- πŸ“ Directory: ${dir}`); - console.log(`- πŸ“ Size limit: ${convertBytes(sizeLimit)} bytes`); - console.log(`- πŸ“ Max size found: ${convertBytes(maxSize, 'MB')}`); - } -} - -// Run the function -checkSize();