-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBox.js
254 lines (230 loc) · 6.73 KB
/
Box.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env node
const fs = require("fs").promises;
const path = require("path");
const Table = require("cli-table");
const rootDirectory = "./";
async function getDirectorySize(dirPath) {
let totalSize = 0;
const files = await fs.readdir(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
if (path.basename(filePath) !== "node_modules") {
totalSize += await getDirectorySize(filePath);
}
} else {
totalSize += stats.size;
}
}
return totalSize;
}
async function getFolderSizes(rootDir) {
const folderSizes = {};
async function calculateFolderSize(dirPath) {
let totalSize = 0;
const files = await fs.readdir(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
const stats = await fs.lstat(filePath);
if (stats.isDirectory()) {
if (stats.isSymbolicLink()) {
continue;
}
totalSize += await calculateFolderSize(filePath);
} else {
totalSize += stats.size;
}
}
return totalSize;
}
const folders = await fs.readdir(rootDir);
for (const folder of folders) {
const folderPath = path.join(rootDir, folder);
const stats = await fs.lstat(folderPath);
if (stats.isDirectory() && !stats.isSymbolicLink()) {
const size = await calculateFolderSize(folderPath);
folderSizes[folder] = size;
}
}
return folderSizes;
}
async function printFolderSizes(
folderSizes,
prefix = "",
table = new Table({
head: ["Folder", "Size"],
})
) {
const filteredFolders = Object.entries(folderSizes).filter(
([folder]) =>
![
"node_modules",
"dist",
"build",
"out",
"lib",
"target",
"bin",
"obj",
"tmp",
"temp",
].includes(folder)
);
for (const [folder, size] of filteredFolders) {
const folderPath = path.join(prefix, folder);
table.push([folderPath, formatFileSize(size)]);
const subFolderSizes = await getFolderSizes(folderPath);
await printFolderSizes(subFolderSizes, folderPath + "/", table);
}
return table;
}
async function main() {
let loadingInterval;
try {
const loadingChars = [
"\x1b[33m|\x1b[0m",
"\x1b[32m/\x1b[0m",
"\x1b[36m -\x1b[0m",
"\x1b[35m\\\x1b[0m",
];
let i = 0;
const spinner = [
"\x1b[33mA\x1b[0m",
"\x1b[32mY\x1b[0m",
"\x1b[35mU\x1b[0m",
"\x1b[36mA\x1b[0m",
"\x1b[31mY\x1b[0m",
"\x1b[34mU\x1b[0m",
"\x1b[33mA\x1b[0m",
"\x1b[32mY\x1b[0m",
"\x1b[35mU\x1b[0m",
"\x1b[36mA\x1b[0m",
"\x1b[31mY\x1b[0m",
"\x1b[34mU\x1b[0m",
];
let currentSpinnerIndex = 0;
loadingInterval = setInterval(() => {
process.stdout.write(
"\r\x1b[31mProcessing \x1b[ " +
" " +
spinner[currentSpinnerIndex] +
loadingChars[currentSpinnerIndex] +
" "
);
currentSpinnerIndex =
((currentSpinnerIndex + 1) % spinner.length) % loadingChars.length;
}, 100);
await new Promise((resolve) => setTimeout(resolve, 3000));
console.log(
"\n\x1b[36mPlease wait, we are surrounding your universe...\x1b[0m\n"
);
} catch (error) {
console.error("An error occurred:", error);
} finally {
clearInterval(loadingInterval);
}
let arr = [];
try {
const rootDirectory = "./";
const totalSize = await getDirectorySize(rootDirectory);
arr.push([
"Total size of the (/) without folders",
formatFileSize(totalSize),
]);
console.log(
`\x1b[32mTotal size of the (/) without folders:\x1b[0m \x1b[36m${formatFileSize(
totalSize
)}\x1b[0m`
);
const excludedFolders = [
"node_modules",
"dist",
"build",
"out",
"lib",
"target",
"bin",
"obj",
"tmp",
"temp",
];
excludedFolders.forEach(async (folder) => {
try {
const folderPath = path.join(rootDirectory, folder);
const folderSize = await getDirectorySize(folderPath);
arr.push([folder, formatFileSize(folderSize)]);
console.log(
`\x1b[32mSize of '${folder}':\x1b[0m \x1b[36m${formatFileSize(
folderSize
)}\x1b[0m`
);
} catch (error) {
if (error.code === "ENOENT") {
} else {
console.error(`Error while processing '${folder}':`, error);
}
}
});
const folderSizes = await getFolderSizes(rootDirectory);
console.log(`\x1b[33mSizes of all folders:\x1b[0m`);
let tabe = await printFolderSizes(folderSizes);
console.log(tabe.toString());
return arr;
} catch (error) {
console.error("Error:", error);
}
}
function formatFileSize(sizeInBytes) {
const units = ["B", "KB", "MB", "GB", "TB"];
let index = 0;
while (sizeInBytes >= 1024 && index < units.length - 1) {
sizeInBytes /= 1024;
index++;
}
return `${sizeInBytes.toFixed(2)} ${units[index]}`;
}
async function saveToCsv(tableData, filePath) {
try {
const csvData =
"Folder,Size\n" + tableData.map((row) => row.join(",")).join("\n");
await fs.writeFile(filePath, csvData);
console.log(`\x1b[33mCSV data saved to ${filePath}\x1b[0m`);
} catch (error) {
console.error("Error while saving CSV:", error);
}
}
const args = process.argv.slice(2);
if (args[0] == "-help") {
console.log("\x1b[36mUsage: ayu-size [options]\x1b[0m");
console.log("\x1b[32mOptions:\x1b[0m");
console.log(
" \x1b[31m-g\x1b[0m Get the size of all folders in the current directory"
);
console.log(" \x1b[31m-d\x1b[0m Save the output to a CSV file");
process.exit(0);
}
if (args.length === 0) {
console.log("\x1b[33mPlease provide options: -g or -d\x1b[0m");
} else if (args[0] !== "-g") {
console.error("\x1b[31m-g is mandatory when using -d\x1b[0m");
console.error("\x1b[31mPlease write: ayu-size -g or ayu-size -g -d\x1b[0m");
process.exit(1);
}
if (args.includes("-g")) {
main()
.then(async (res) => {
const folderSizes = await getFolderSizes(rootDirectory);
const table = await printFolderSizes(folderSizes);
if (args.includes("-d")) {
await saveToCsv([...table, ...res], "Folder-sizes.csv");
}
console.log("\x1b[32mWe are Done!\x1b[0m");
})
.catch((error) => {
console.error("An error occurred:", error);
});
} else {
console.error("\x1b[31mPlease provide options: -g or -d\x1b[0m");
process.exit(1);
}