const path = require('path');
const fs = require('fs');
const writePkg = require('write-pkg');
const getPackages = require('./utils/getPackages');

const cwd = process.cwd();
const ignoreDirPath = ['.DS_Store'];

const filePath = path.resolve(cwd, 'package.json');
// const packagesDir = path.resolve(cwd, 'packages');
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));

delete json['size-limit'];

let componentsNames = getPackages();
componentsNames = componentsNames.filter((dir) => ignoreDirPath.indexOf(dir) === -1);

(async () => {
  const sizeLimitConfig = [];
  componentsNames.forEach((component) => {
    const [shortName, p] = component.split('!');
    sizeLimitConfig.push({
      path: `packages/${p}/${shortName}/lib/**/*.js`,
      limit: '2 s',
      webpack: false,
      running: false,
    });
    sizeLimitConfig.push({
      path: `packages/${p}/${shortName}/es/**/*.js`,
      limit: '2 s',
      webpack: false,
      running: false,
    });
  });

  await writePkg(cwd, { ...json, 'size-limit': sizeLimitConfig });
})();
