const fs = require('fs')
var pako = require('pako');

// 压缩导出的数据
function compress_data(filename) {

    const file_content = fs.readFileSync(`./../res/${filename}`, { encoding: 'utf-8' });

    //压缩
    let namelist = filename.split('.');
    let name = namelist[0];
    console.log(`${filename}开始压缩...`);
    var binaryString = pako.deflate(file_content, { to: 'string' });
    console.log(`${filename}开始写入...`);
    fs.writeFileSync(`./../dist/${name}.manifest`, binaryString);

    //解压
    // console.log(`${filename}开始解压...`);
    // var jsonObj = JSON.parse(pako.inflate(binaryString, { to: 'string' }));
    // fs.writeFileSync(`./../dist/${name}.json`, JSON.stringify(jsonObj));
    // console.log(`${filename}开始写入...`);


}

function compress_data_fileDir(){
    fs.readdir('./../res',(error, files)=>{
        console.log(files);
        files.forEach((filename, index) => {
            console.log(filename);
            if (filename.indexOf(".DS_Store") == 0) {
                return;
            }
            let namelist = filename.split('.');
            if(namelist[namelist.length - 1] = 'json'){
                compress_data(filename)
            }
            
        })

    })
}

// 导出游戏的配置数据
async function asyncCall() {
    console.log('----执行压缩中---');
    compress_data_fileDir()
}

asyncCall();