/**
 * 用于删除旧工程中关于apps模块(如edf)等配置文件，新工程自动搜索
 * 包括theme目录、index.js、index.less这些import配置文件
 */
const fs = require('fs');

const JSON_APPS_PATH = './src/jsonApps'

const handle = (path, name) => {
    // 同时存在theme目录、index.js、index.less的目录为模块目录
    if (fs.existsSync(`${path}/theme`) && fs.existsSync(`${path}/index.js`) && fs.existsSync(`${path}/index.less`)) {
        fs.unlinkSync(`${path}/index.js`)
        console.log(`删除模块${name}的index.js`)
        fs.unlinkSync(`${path}/index.less`)
        console.log(`删除模块${name}的index.less`)
        delDir(`${path}/theme`)
        console.log(`删除模块${name}的theme`)
    }
    search(path);
}

const delDir = (path) => {
    const list = fs.readdirSync(path)
    list.forEach((name) => {
        let childPath = `${path}/${name}`;
        const stats = fs.statSync(childPath)
        // 判断是文件还是文件夹
        if (stats.isFile(childPath)) {
            // 当前为文件，则删除文件
            fs.unlinkSync(childPath)
        } else {
            // 当前为文件夹，则递归调用自身
            delDir(childPath)
        }
    })
    // 删除空文件夹
    fs.rmdirSync(path)
}

const search = (path) => {
    const dirs = fs.readdirSync(path, { withFileTypes: true })
    dirs.forEach(dir => {
        if (dir.isDirectory()) {
            let childPath = `${path}/${dir.name}`;
            handle(childPath,dir.name);
        }
    })
}
search(JSON_APPS_PATH);



