//生成res.json

//遍历资源文件夹，生成
var fs = require('fs');
var path = require("path");
var readPath = "./resource/"
var files = fs.readdirSync(readPath);
var obj = { groups: [] };//每项包括keys合name

files.forEach(function (file) {
    //路径
    let fPath = path.join(readPath, file);
    //只处理文件夹
    if (fs.statSync(fPath).isDirectory()) {
        //继续读每个子文件夹，json和png名字有相同的，只留json，
        var sonFiles = fs.readdirSync(fPath);
        //没有文件
        if (!sonFiles.length) return
        //取出所有json
        var jsons = sonFiles.filter((f) => { return f.substr(-5) == ".json" })
        //去掉json所带png的图片
        sonFiles = sonFiles.filter((f) => { return jsons.indexOf(f.substring(0, f.length - 4) + ".json") == -1 })
        //去掉mac上的缓存文件
        sonFiles = sonFiles.filter((f) => { return f != '.DS_Store' })
        var group = {
            keys: "",
            name: file
        }
        for (var i = 0; i < sonFiles.length; i++) {
            if (i != 0) group.keys += ",";
            group.keys += sonFiles[i]
        }
        obj.groups.push(group)
    }
})
obj.path="./resource/"
console.log("资源更新完成")
//生成json
fs.writeFileSync(readPath + "res.json", JSON.stringify(obj, "", "\t"));
//TS也更新
var endPath = './src/';
var endFile = `export const ResJson = ${JSON.stringify(obj, "", "\t")}`
fs.writeFileSync(endPath + "ResJson.ts", endFile);