/**
 * 用于自动生成入口文件，不用每次新加一个组件还要在index.js中配置局部注册组件
 */
import { onReadDirectory, onWriteFile } from "./utils";

const onCreateContent = (files: string[]) => {
  let fileContent = files.reduce((acc, item) => {
    return (acc += `import ${item} from "./components/${item}";`);
  }, "");
  fileContent += `const components = [${files}]`;
  fileContent += `
  const install = (app) => {
    components.forEach((i) => {
      app.use(i);
    });
  };
  const weilingMobileUI = {
    version: "0.0.1",
    install,
  };`;
  fileContent += `export {
    install,
    ${files}
  };
  export default weilingMobileUI;`;
  return fileContent;
};

export const createIndexJS = async (entryPath: string, compDirPath: string) => {
  const files = await onReadDirectory(compDirPath);
  const fileContent = onCreateContent(files as string[]);
  await onWriteFile(entryPath, fileContent);
};
