const fs = require('fs');
const path = require('path');

const root = path.join(__dirname, '..');
const outputDir = path.join(root, 'dist-release-final');

const exactNames = new Set([
  'win-unpacked',
  'release-assets',
  'nsis-web',
]);

const removableFilePatterns = [
  /-win-setup-x64\.exe$/i,
  /-win-setup-x64\.exe\.baiduyun\.uploading\.cfg$/i,
  /^__uninstaller-nsis-.*\.exe$/i,
  /\.nsis\.7z$/i,
  /\.blockmap$/i,
];

function remove(target) {
  if (fs.existsSync(target)) {
    fs.rmSync(target, { recursive: true, force: true });
    console.log(`[clean-oem-output] removed ${target}`);
  }
}

function main() {
  fs.mkdirSync(outputDir, { recursive: true });

  for (const name of fs.readdirSync(outputDir)) {
    const full = path.join(outputDir, name);
    const stat = fs.statSync(full);
    if (stat.isDirectory() && exactNames.has(name)) {
      remove(full);
      continue;
    }
    if (stat.isFile() && removableFilePatterns.some(pattern => pattern.test(name))) {
      remove(full);
    }
  }
}

main();
