forked from pheralb/svgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (62 loc) · 1.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* 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 >= sizeLimit) {
maxFiles.push({
filename: file,
size: stats.size
});
if (stats.size > maxSize) {
maxSize = stats.size;
}
}
}
if (maxFiles.length === 0) {
message = `- ✅ All files are smaller than ${convertBytes(sizeLimit)}`;
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`);
if (maxSize > 0) {
console.log(`- 🔔 Max size found: ${convertBytes(maxSize, 'KB')}`);
}
}
}
// Run the function
checkSize();