const fs = require("fs");
const path = require("path");

const processData = process.argv;
// console.log("processData", processData);
const filePath = processData[2]
  ? `../components/${processData[2]}`
  : `../components`;
const pageDir = path.resolve(__dirname, filePath);

const createIndex = () => {
  const directoryContent = fs.readdirSync(pageDir);
  //   console.log("directoryContent >>", directoryContent);
  if (directoryContent.length) {
    const content = directoryContent
      .map((item) => {
        if (item.endsWith(".jsx")) {
          const fileName = item.replace(".jsx", "");
          return `export const ${fileName} = lazy(() => import("./${fileName}"));`;
          // console.log("fileContent", fileContent);
        }
      })
      .filter(Boolean);

    fs.writeFileSync(
      path.resolve(pageDir, "index.js"),
      `
      import { lazy } from "react";

      ${content.join("\n")}
      `,
      { encoding: "utf-8" }
    );
  }
};

createIndex();
