{"size": 2274, "ext": "js", "max_stars_repo_path": "src/utils/getPagesInfo.js", "max_stars_repo_name": "helloskynet/craco-plugin-multipage", "max_stars_repo_stars_event_min_datetime": "2021-04-29T06:14:57.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-07T07:39:05.000Z", "max_issues_repo_path": "src/utils/getPagesInfo.js", "max_issues_repo_name": "helloskynet/craco-plugin-multipage", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/utils/getPagesInfo.js", "max_forks_repo_name": "helloskynet/craco-plugin-multipage", "max_forks_repo_forks_event_min_datetime": "2021-07-07T07:39:08.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-07T07:39:08.000Z"}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 22.0776699029, "max_line_length": 81, "alphanum_fraction": 0.5773966579}
import fs from "fs";
import path from "path";
import { flatten } from "lodash";
import { match } from "path-to-regexp";

let config = null;
let titleMap = null;

/**
 * 遍历指定目录
 * @param {*} pagePath 遍历的目录
 * @param {*} dir
 */
function getPagesInfo(pagePath, dir = "") {
  if (dir) {
    // 忽略的页面
    if (
      config.pagesRegexp.ignore.length > 0 &&
      config.pagesRegexp.ignore.some(
        (item) => !!item.exec(dir.replace(/\\/g, "/"))
      )
    ) {
      return [];
    }
    // 指定的页面
    if (
      config.pagesRegexp.pages.length > 0 &&
      !config.pagesRegexp.pages.some(
        (item) => !!item.exec(dir.replace(/\\/g, "/"))
      )
    ) {
      return [];
    }
  }

  const fileList = fs.readdirSync(pagePath);
  const all = [];
  const directories = [];

  fileList.forEach((fileName) => {
    if (!fileName.startsWith(".")) {
      all.push(fileName);
      if (fs.statSync(path.resolve(pagePath, fileName)).isDirectory()) {
        directories.push(fileName);
      }
    }
  });
  // 文件夹中存在文件
  if (directories.length !== all.length && dir) {
    // 如果 文件夹中存在index.js文件 表示这是个页面
    if (fs.existsSync(path.resolve(config.paths.appSrc, dir, "index.js"))) {
      const title = getPageTitle(dir);
      return {
        name: dir,
        dir,
        title,
        entry: path.resolve(config.paths.appSrc, dir, "index.js"),
      };
    }
    return [];
  }

  // 返回这个文件夹下 所有页面的信息
  return flatten(
    directories.map((fileName) => {
      const filePath = path.resolve(pagePath, fileName);
      return getPagesInfo(
        filePath,
        path.relative(config.paths.appSrc, filePath)
      );
    })
  );
}

/**
 * 获取页面的title
 * @param {string} dir
 */
function getPageTitle(dir) {
  if (config.options.pageTitle && typeof config.options.pageTitle === "object") {
    const match1 = match(dir.replace(/\\/g, "/"), {
      decode: decodeURIComponent,
    });
    if (!titleMap) {
      titleMap = Object.entries(config.options.pageTitle);
    }
    const title = titleMap.find((item) => {
      return match1(item[0]);
    });
    if (title) {
      return title[1];
    }
  }
  return config.options.defaultTitle;
}

function getPages(initConfig, pagePath) {
  config = initConfig;
  return getPagesInfo(pagePath);
}

export default getPages;
