const fs = require('fs');
const glob = require("glob");
const pathVars = require("./pathVars");

function getPages() {
    let files = glob.sync(pathVars.srcPath + "/pages/*/index.html");
    //let files = glob.sync(pathVars.srcPath + "/pages/light/index.html");

    //console.log(files);
    let pageAry = [];
    files.forEach((filePath) => {
        //将单个文件路径(/src/pages/news/detail/index.html)路径拆分成数组
        let tempAry = filePath.split('/');
        let chunkName = tempAry[tempAry.length - 2];
        //let htmlName = chunkName + "/index.html";
        let htmlName = chunkName + ".html";
        let entryJs = pathVars.pagesPath + "/" + chunkName + "/index.js";

        let obj = {};
        obj["filePath"] = filePath;
        obj["chunkName"] = chunkName;
        obj["htmlName"] = htmlName;
        obj["entryJs"] = entryJs;
        pageAry.push(obj);
    });
    return pageAry;
}

function getCharts(){
    let files = glob.sync(pathVars.srcPath + "/charts/chart*/Chart*.js");
    let chartsAry=[];
    files.forEach((filePath) => {
        let tempAry = filePath.split('/');
        let chunkName = tempAry[tempAry.length - 2];
        let obj = {};
        obj["chunkName"] = chunkName;
        obj["entryJs"] = filePath;
        chartsAry.push(obj);
    });
    return chartsAry;
}

function getChartsTest() {
    let template = pathVars.chartsTestPath + "/template.html"
    let files = glob.sync(pathVars.srcPath + "/charts_test/chart*/index.js");
    let chartsAry = [];
    files.forEach((filePath) => {
        let tempAry = filePath.split('/');
        let chunkName = tempAry[tempAry.length - 2];

        //目录下有index.html时,使用index.html作为html模版页,没有时使用template.html作为html模版页
        let htmlPath = pathVars.srcPath + "/charts_test/" + chunkName + "/index.html";
        try {
            fs.statSync(htmlPath).isFile();
        } catch (e) {
            htmlPath = template;
        }
        let obj = {};
        obj["chunkName"] = chunkName;
        obj["entryJs"] = filePath;
        obj["htmlName"] = chunkName + ".html";
        obj["htmlPath"] = htmlPath;
        chartsAry.push(obj);
    });
    return chartsAry;
}

module.exports = {
    getPages,
    getCharts,
    getChartsTest
};