const fs = require('fs');
const path = require('path');
const componentDir = 'src/components';
const cModuleNames = fs.readdirSync(path.resolve(componentDir));

const tcomponentDir = 'src/themeComponents';
const tModuleNames = fs.readdirSync(path.resolve(tcomponentDir));

const bcomponentDir = 'src/businessComponents';
const bModuleNames = fs.readdirSync(path.resolve(bcomponentDir));

function run() {
  let entryText = '';
  let dtsText = '';
  cModuleNames.forEach((name) => {
    if (fs.statSync(path.resolve(`${componentDir}/${name}`)).isDirectory()) {
      entryText += `export { default as ${name} } from './components/${name}';\n`;
      dtsText += `export { default as ${name} } from './src/components/${name}';\n`;
    }
  });
  tModuleNames.forEach((name) => {
    if (fs.statSync(path.resolve(`${tcomponentDir}/${name}`)).isDirectory()) {
      entryText += `export { default as ${name} } from './themeComponents/${name}';\n`;
      dtsText += `export { default as ${name} } from './src/themeComponents/${name}';\n`;
    }
  });
  bModuleNames.forEach((name) => {
    if (fs.statSync(path.resolve(`${bcomponentDir}/${name}`)).isDirectory()) {
      entryText += `export { default as ${name} } from './businessComponents/${name}';\n`;
      dtsText += `export { default as ${name} } from './src/businessComponents/${name}';\n`;
    }
  });
  fs.writeFileSync(path.resolve('src/entry.js'), entryText);
  fs.writeFileSync(path.resolve('./index.d.ts'), dtsText);
}

run();
