const fs = require('fs');
const { join } = require('path');
const fg = require('fast-glob');
const getPackages = require('./utils/getPackages');

const pkgList = getPackages();

pkgList.map(async (pkg) => {
  const [name, path] = pkg.split('!');
  const baseUrl = `${join(__dirname, '../', 'packages')}/${path}/${name}/src`;
  const lessFiles = await fg(`${baseUrl}/**/*.less`, {
    ignore: ['**/demos/**'],
    deep: 5,
  });
  const importFiles = lessFiles.map((lessPath) => {
    return `@import "../es${lessPath.replace(baseUrl, '')}";`;
  });

  const distPath = `${join(__dirname, '../', 'packages', path, name, 'dist', `${name}.less`)}`;
  if (fs.existsSync(distPath)) {
    fs.writeFileSync(distPath, importFiles.join('\n'));
  }
});
