保存为 obfuscate-all.js文件,建议放在 zfb-mini-tools同级目录
const fs = require('fs');
const path = require('path');
const { minify } = require('terser');
// 主文件夹路径
const mainDir = path.resolve(__dirname, 'zfb-mini-tools'); // 替换成你的主文件夹路径
// 生成随机混淆配置
function generateRandomConfig() {
return {
compress: Math.random() > 0.5 ? true : {
drop_console: Math.random() > 0.5,
drop_debugger: Math.random() > 0.5
},
mangle: Math.random() > 0.5 ? true : {
toplevel: Math.random() > 0.5
}
};
}
// 递归读取目录并混淆文件
function obfuscateDirectory(directory, config) {
const files = fs.readdirSync(directory);
files.forEach(file => {
const filePath = path.join(directory, file);
if (fs.lstatSync(filePath).isDirectory()) {
if (file === '.git' || file === '.idea') {
console.log(`Skipping directory: ${filePath}`);
} else {
obfuscateDirectory(filePath, config);
}
} else if (file.endsWith('.js')) {
const code = fs.readFileSync(filePath, 'utf8');
minify(code, config).then(result => {
fs.writeFileSync(filePath, result.code, 'utf8');
console.log(`Obfuscated ${filePath}`);
}).catch(error => {
console.error(`Error obfuscating ${filePath}:`, error);
});
}
});
}
// 遍历主文件夹中的每个子文件夹
function processProjects(mainDir) {
const subdirs = fs.readdirSync(mainDir);
subdirs.forEach(subdir => {
const subdirPath = path.join(mainDir, subdir);
if (fs.lstatSync(subdirPath).isDirectory()) {
console.log(`Processing project: ${subdir}`);
const config = generateRandomConfig();
obfuscateDirectory(subdirPath, config);
}
});
}
// 开始处理所有项目
processProjects(mainDir);