Skip to content

Instantly share code, notes, and snippets.

@nsisodiya
Created September 23, 2020 06:33
Show Gist options
  • Save nsisodiya/90f90ca4fcddb7879c292e1bd2a26e56 to your computer and use it in GitHub Desktop.
Save nsisodiya/90f90ca4fcddb7879c292e1bd2a26e56 to your computer and use it in GitHub Desktop.
genBuildFileSize.js
const fs = require('fs').promises;
const path = require('path');
const prettyBytes = require('pretty-bytes');
const buildDir = 'build';
const blackListedFiles = ['build/git-log.json'];
const blackListedDirs = ['build/report'];
const isArrayHas = (arr, str) => {
const outOfIndex = -1;
return arr.indexOf(str) !== outOfIndex;
};
async function walk(dir) {
let files = await fs.readdir(dir);
//console.log('files', files);
files = await Promise.all(
files.map(async (file) => {
const filePath = path.join(dir, file);
const stats = await fs.stat(filePath);
const size = stats.size;
const sizep = prettyBytes(stats.size);
if (stats.isDirectory()) {
if (!isArrayHas(blackListedDirs, filePath)) {
return walk(filePath);
}
} else if (stats.isFile()) {
if (!isArrayHas(blackListedFiles, filePath)) {
var fA = filePath.split('.');
if (fA[fA.length - 1] !== 'map') {
const fPath = filePath
.split('.')
.filter((x) => !new RegExp(/^([a-f0-9]){8}$/).test(x))
.join('.');
return { filePath: fPath, size, sizep };
}
}
}
})
);
return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
(async () => {
try {
const list = await walk(buildDir);
console.warn('Welcome to File Size Checked Utility');
await fs.writeFile(
'report/bundle-size.json',
JSON.stringify(
list.filter((x) => x !== undefined),
null,
'\t'
)
);
console.warn('File written - report/bundle-size.json');
} catch (error) {
console.error(error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment