Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit d19578b

Browse files
danielreardenIvanGoncharov
authored andcommitted
Move showStats function to utils
1 parent 8e2571c commit d19578b

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

resources/build.js

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const assert = require('assert');
88

99
const babel = require('@babel/core');
1010

11-
const { rmdirRecursive, readdirRecursive } = require('./utils');
11+
const { rmdirRecursive, readdirRecursive, showStats } = require('./utils');
1212

1313
if (require.main === module) {
1414
rmdirRecursive('./dist');
@@ -68,49 +68,3 @@ function buildPackageJSON() {
6868

6969
return packageJSON;
7070
}
71-
72-
function showStats() {
73-
const fileTypes = {};
74-
let totalSize = 0;
75-
76-
for (const filepath of readdirRecursive('./dist')) {
77-
const name = filepath.split(path.sep).pop();
78-
const [base, ...splitExt] = name.split('.');
79-
const ext = splitExt.join('.');
80-
81-
const filetype = ext ? '*.' + ext : base;
82-
fileTypes[filetype] = fileTypes[filetype] || { filepaths: [], size: 0 };
83-
84-
const { size } = fs.lstatSync(path.join('./dist', filepath));
85-
totalSize += size;
86-
fileTypes[filetype].size += size;
87-
fileTypes[filetype].filepaths.push(filepath);
88-
}
89-
90-
let stats = [];
91-
for (const [filetype, typeStats] of Object.entries(fileTypes)) {
92-
const numFiles = typeStats.filepaths.length;
93-
94-
if (numFiles > 1) {
95-
stats.push([filetype + ' x' + numFiles, typeStats.size]);
96-
} else {
97-
stats.push([typeStats.filepaths[0], typeStats.size]);
98-
}
99-
}
100-
stats.sort((a, b) => b[1] - a[1]);
101-
stats = stats.map(([type, size]) => [type, (size / 1024).toFixed(2) + ' KB']);
102-
103-
const typeMaxLength = Math.max(...stats.map((x) => x[0].length));
104-
const sizeMaxLength = Math.max(...stats.map((x) => x[1].length));
105-
for (const [type, size] of stats) {
106-
console.log(
107-
type.padStart(typeMaxLength) + ' | ' + size.padStart(sizeMaxLength),
108-
);
109-
}
110-
111-
console.log('-'.repeat(typeMaxLength + 3 + sizeMaxLength));
112-
const totalMB = (totalSize / 1024 / 1024).toFixed(2) + ' MB';
113-
console.log(
114-
'Total'.padStart(typeMaxLength) + ' | ' + totalMB.padStart(sizeMaxLength),
115-
);
116-
}

resources/utils.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,56 @@ function readdirRecursive(dirPath, opts = {}) {
6969
return result;
7070
}
7171

72+
function showStats() {
73+
const fileTypes = {};
74+
let totalSize = 0;
75+
76+
for (const filepath of readdirRecursive('./dist')) {
77+
const name = filepath.split(path.sep).pop();
78+
const [base, ...splitExt] = name.split('.');
79+
const ext = splitExt.join('.');
80+
81+
const filetype = ext ? '*.' + ext : base;
82+
fileTypes[filetype] = fileTypes[filetype] || { filepaths: [], size: 0 };
83+
84+
const { size } = fs.lstatSync(path.join('./dist', filepath));
85+
totalSize += size;
86+
fileTypes[filetype].size += size;
87+
fileTypes[filetype].filepaths.push(filepath);
88+
}
89+
90+
let stats = [];
91+
for (const [filetype, typeStats] of Object.entries(fileTypes)) {
92+
const numFiles = typeStats.filepaths.length;
93+
94+
if (numFiles > 1) {
95+
stats.push([filetype + ' x' + numFiles, typeStats.size]);
96+
} else {
97+
stats.push([typeStats.filepaths[0], typeStats.size]);
98+
}
99+
}
100+
stats.sort((a, b) => b[1] - a[1]);
101+
stats = stats.map(([type, size]) => [type, (size / 1024).toFixed(2) + ' KB']);
102+
103+
const typeMaxLength = Math.max(...stats.map((x) => x[0].length));
104+
const sizeMaxLength = Math.max(...stats.map((x) => x[1].length));
105+
for (const [type, size] of stats) {
106+
console.log(
107+
type.padStart(typeMaxLength) + ' | ' + size.padStart(sizeMaxLength),
108+
);
109+
}
110+
111+
console.log('-'.repeat(typeMaxLength + 3 + sizeMaxLength));
112+
const totalMB = (totalSize / 1024 / 1024).toFixed(2) + ' MB';
113+
console.log(
114+
'Total'.padStart(typeMaxLength) + ' | ' + totalMB.padStart(sizeMaxLength),
115+
);
116+
}
117+
72118
module.exports = {
73119
exec,
74120
execAsync,
75121
rmdirRecursive,
76122
readdirRecursive,
123+
showStats,
77124
};

0 commit comments

Comments
 (0)