
var fs = require("fs");
var path = require('path');
var del = require('del');

var pathName = "./resource";

var inPath = "./resource/common/";

{
    key: ["aa", "bb"]
}
var hash = {};//记录名字和所在文件夹

var files = fs.readdirSync(pathName);

files.forEach(function (file) {
    //文件common，不处理
    if (file == "common") return
    //路径
    let fPath = path.join(pathName, file);
    //只处理文件夹
    if (fs.statSync(fPath).isDirectory()) {
        var sonFiles = fs.readdirSync(fPath);
        //没有文件
        if (!sonFiles.length) return;
        sonFiles.forEach(function (s) {
            if (!hash[s]) hash[s] = [];
            hash[s + ""].push(file);
        })
    }
})

//遍历
for (var key in hash) {
    //有重复的，拷贝到common，删除原文件夹里的文件，
    if (hash[key].length > 1) {
        // 拷贝到common
        fs.writeFileSync(inPath + key, fs.readFileSync(pathName + "/" + hash[key][0] + "/" + key));
        console.log("重复资源：" + key)
        //删除原文件夹里的文件，
        hash[key].forEach(function (s) {
            del(pathName + "/" + s + "/" + key)
        })
    }
}

console.log("重复资源处理完成")