const fs = require("fs");
const path = require('path');
const JSZip = require("jszip");
const folderPath = "./configs"
const start = () => {
  const files = fs.readdirSync(folderPath);

  // 过滤出所有的 JSON 文件
  const jsonFiles = files.filter(file => path.extname(file) === '.json');

  // 并行读取所有的 JSON 文件内容
  const combinedJson = {}
  jsonFiles.forEach(file => {
    combinedJson[path.basename(file, '.json')] = JSON.parse(fs.readFileSync(path.join(folderPath, file), 'utf8'))
  });
  return compress(JSON.stringify(combinedJson, null, 2))
}
const compress = json =>new Promise(resolve =>  {
  const zip = new JSZip();
  // 将JSON文件内容添加到ZIP实例中
  zip.file('config.json', json, {compression: "DEFLATE", compressionOptions: {level: 9}});

  // 生成ZIP文件内容
  zip.generateNodeStream({type: 'nodebuffer', streamFiles: true})
    .pipe(fs.createWriteStream('config.xml'))
    .on('finish', function () {
      console.log('ZIP file created.');
      resolve()
    });

})
module.exports = {start}
