const fs = require('fs'); const path = require('path'); // 获取项目根目录(脚本在 scripts/encrypt 目录下,需要向上两级) const projectRoot = path.resolve(__dirname, '../..'); // 读取模块映射配置(从 lib 目录读取) const moduleMapPath = path.join(projectRoot, 'lib', 'module-map.json'); let moduleMap = {}; if (fs.existsSync(moduleMapPath)) { try { moduleMap = JSON.parse(fs.readFileSync(moduleMapPath, 'utf8')); } catch (error) { console.warn(`⚠️ 读取模块映射配置失败: ${error.message},将清理所有 .jsc 文件\n`); } } // 文件映射函数:根据配置返回加密后的文件名(跨平台兼容) function getEncryptedFileName(originalPath) { // 使用 path 模块处理路径,确保跨平台兼容 const normalizedPath = originalPath.replace(/\\/g, '/'); // 统一使用正斜杠 const parts = normalizedPath.split('/'); const fileName = parts[parts.length - 1].replace('.js', ''); const dirName = parts[parts.length - 2]; // 检查是否有映射配置 if (moduleMap.mappings) { if (dirName === 'design1' && moduleMap.mappings.design1 && moduleMap.mappings.design1[fileName]) { return moduleMap.mappings.design1[fileName] + '.jsc'; } if (dirName === 'design2' && moduleMap.mappings.design2 && moduleMap.mappings.design2[fileName]) { return moduleMap.mappings.design2[fileName] + '.jsc'; } if (dirName === 'lib' && moduleMap.mappings.main && moduleMap.mappings.main[fileName]) { return moduleMap.mappings.main[fileName] + '.jsc'; } } // 默认:原文件名 + 'c' return fileName + '.jsc'; } const filesToClean = [ 'lib/index.js', 'lib/design1.js', 'lib/design2.js', 'lib/design1/core.js', 'lib/design1/output.js', 'lib/design1/ui.js', 'lib/design2/core.js', 'lib/design2/output.js', 'lib/design2/ui.js' ]; console.log('正在删除所有 .jsc 文件...\n'); let deletedCount = 0; let notFoundCount = 0; filesToClean.forEach(filePath => { // 跨平台兼容的路径处理 const fullPath = path.join(projectRoot, filePath); const dir = path.dirname(fullPath); const encryptedFileName = getEncryptedFileName(filePath); // 尝试删除映射后的文件名 const mappedJscPath = path.join(dir, encryptedFileName); const defaultJscPath = fullPath + 'c'; // 使用 path.resolve 进行规范化比较,确保跨平台兼容 const normalizedDefault = path.resolve(defaultJscPath); const normalizedMapped = path.resolve(mappedJscPath); if (fs.existsSync(mappedJscPath)) { try { fs.unlinkSync(mappedJscPath); console.log(`✅ 已删除: ${mappedJscPath}`); deletedCount++; } catch (error) { console.error(`❌ 删除失败: ${mappedJscPath} - ${error.message}`); } } // 也尝试删除默认名称的 .jsc 文件(兼容旧版本) if (fs.existsSync(defaultJscPath) && normalizedMapped !== normalizedDefault) { try { fs.unlinkSync(defaultJscPath); console.log(`✅ 已删除: ${defaultJscPath}`); deletedCount++; } catch (error) { console.error(`❌ 删除失败: ${defaultJscPath} - ${error.message}`); } } if (!fs.existsSync(mappedJscPath) && !fs.existsSync(defaultJscPath)) { notFoundCount++; } }); console.log(`\n完成!已删除 ${deletedCount} 个文件,${notFoundCount} 个文件不存在`);