const fs = require('fs');
const DATA = require('../stats.json');

function getFileSize(file) {
  const { size } = fs.statSync(`.next/${file}`);
  console.log(`${file} is ${size} bytes`);
  return size;
}

const fileSizes = DATA.chunks.map(element => getFileSize(element.files[0]));

const res = fileSizes.reduce((total, num) => {
  return Number(total) + Number(num);
}, 0);

fs.writeFile(
  './public/size.json',
  JSON.stringify({
    size_in_bytes: res.toFixed(2),
    size_in_kilobytes: (res * 0.001).toFixed(2),
  }),
  'utf8'
);