/**
 * @file 各模块文件数量统计脚本(从 BI 搬过来的)
 */
let fs = require('fs');
let path = require('path');

const rootPath = path.resolve(__dirname, '../packages');
// 获取所有的模块名
const modules = fs.readdirSync(rootPath);
const result = [];

let jsFileCount = 0;
let tsFileCount = 0;
let tsxFileCount = 0;

const file = async filePath => {
    if (
        filePath.endsWith('.js') &&
        filePath.indexOf('/dist/') < 0 &&
        filePath.indexOf('/webpack/') < 0 &&
        filePath.indexOf('/webui-bi/private/') < 0
    ) {
        console.log(jsFileCount + ':' + path.normalize(filePath));
        jsFileCount++;
    }
    if (filePath.endsWith('.ts')) {
        tsFileCount++;
    }
    if (filePath.endsWith('.tsx')) {
        tsxFileCount++;
    }
};

const reset = () => {
    jsFileCount = 0;
    tsFileCount = 0;
    tsxFileCount = 0;
};

// 递归所有文件夹统计
const codeStat = async (pt, lineFun) => {
    let files = fs.readdirSync(pt);
    files
        .map(file => {
            return `${pt}/${file}`;
        })
        .forEach(file => {
            let stat = fs.statSync(file);
            if (stat.isDirectory()) {
                if (
                    file.indexOf('__test__') > -1 ||
                    file.indexOf('__point__') > -1 ||
                    file.indexOf('third') > -1 ||
                    file.indexOf('__e2e__') > -1 ||
                    file.indexOf('__integration__') > -1 ||
                    file.indexOf('node_modules') > -1
                ) {
                    return;
                }
                codeStat(file, lineFun);
                return;
            }
            if (file.indexOf('.DS_Store') > -1) {
                return;
            }
            lineFun(file);
        });
};

const statisticFile = () => {
    modules.forEach(i => {
        codeStat(`${rootPath}/${i}/src`, file);
        result.push({
            name: i,
            jsFileCount,
            tsFileCount,
            tsxFileCount,
            allFileCount: jsFileCount + tsFileCount + tsxFileCount,
        });
        reset();
    });

    console.table(result);
};

statisticFile();
module.exports = {
    statisticFile,
};