163 lines
5.8 KiB
JavaScript
163 lines
5.8 KiB
JavaScript
/**
|
||
* 清理脚本 - 删除源 JS 文件(保留 .jsc 文件)
|
||
*
|
||
* 使用方法:
|
||
* node cleanup.js
|
||
*
|
||
* 注意:此脚本会自动备份源文件到 .backup 目录
|
||
*/
|
||
|
||
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},将使用默认文件名\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';
|
||
}
|
||
|
||
// 需要删除的源文件列表(对应的 .jsc 文件会被保留)
|
||
const filesToDelete = [
|
||
'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',
|
||
];
|
||
|
||
// 备份目录
|
||
const backupDir = path.join(projectRoot, '.backup');
|
||
|
||
console.log('开始清理源文件...\n');
|
||
console.log('⚠️ 警告:此操作将删除源 JS 文件,只保留 .jsc 文件!\n');
|
||
|
||
// 创建备份目录
|
||
if (!fs.existsSync(backupDir)) {
|
||
fs.mkdirSync(backupDir, { recursive: true });
|
||
console.log(`✅ 已创建备份目录: ${backupDir}\n`);
|
||
}
|
||
|
||
let deletedCount = 0;
|
||
let notFoundCount = 0;
|
||
let errorCount = 0;
|
||
let backedUpCount = 0;
|
||
|
||
filesToDelete.forEach(filePath => {
|
||
try {
|
||
const fullPath = path.join(projectRoot, filePath);
|
||
const dir = path.dirname(fullPath);
|
||
|
||
// 检查默认名称的 .jsc 文件
|
||
const defaultJscPath = fullPath + 'c';
|
||
// 检查映射后的 .jsc 文件
|
||
const encryptedFileName = getEncryptedFileName(filePath);
|
||
const mappedJscPath = path.join(dir, encryptedFileName);
|
||
|
||
// 检查是否存在 .jsc 文件(默认或映射后的)
|
||
const jscExists = fs.existsSync(defaultJscPath) || fs.existsSync(mappedJscPath);
|
||
|
||
if (!jscExists) {
|
||
console.warn(`⚠️ 跳过: ${filePath} (对应的 .jsc 文件不存在)`);
|
||
notFoundCount++;
|
||
return;
|
||
}
|
||
|
||
// 备份源文件
|
||
if (fs.existsSync(fullPath)) {
|
||
const backupPath = path.join(backupDir, filePath);
|
||
const backupDirPath = path.dirname(backupPath);
|
||
|
||
// 确保备份目录存在
|
||
if (!fs.existsSync(backupDirPath)) {
|
||
fs.mkdirSync(backupDirPath, { recursive: true });
|
||
}
|
||
|
||
// 复制文件到备份目录
|
||
fs.copyFileSync(fullPath, backupPath);
|
||
console.log(`📦 已备份: ${filePath} -> .backup/${filePath}`);
|
||
backedUpCount++;
|
||
|
||
// 删除源文件
|
||
fs.unlinkSync(fullPath);
|
||
console.log(`✅ 已删除: ${filePath}`);
|
||
deletedCount++;
|
||
} else {
|
||
console.log(`ℹ️ 不存在: ${filePath} (可能已删除)`);
|
||
notFoundCount++;
|
||
}
|
||
} catch (error) {
|
||
console.error(`❌ 处理失败: ${filePath}`);
|
||
console.error(` 错误: ${error.message}`);
|
||
errorCount++;
|
||
}
|
||
});
|
||
|
||
// 创建备份信息文件
|
||
const backupInfo = {
|
||
timestamp: new Date().toISOString(),
|
||
files: filesToDelete.filter((filePath, index) => {
|
||
const fullPath = path.join(projectRoot, filePath);
|
||
return fs.existsSync(path.join(backupDir, filePath));
|
||
})
|
||
};
|
||
|
||
const backupInfoPath = path.join(backupDir, 'backup-info.json');
|
||
fs.writeFileSync(backupInfoPath, JSON.stringify(backupInfo, null, 2));
|
||
console.log(`\n📝 已创建备份信息文件: .backup/backup-info.json`);
|
||
|
||
console.log('\n' + '='.repeat(50));
|
||
console.log(`清理完成!`);
|
||
console.log(` 已备份: ${backedUpCount}`);
|
||
console.log(` 已删除: ${deletedCount}`);
|
||
console.log(` 未找到: ${notFoundCount}`);
|
||
console.log(` 错误: ${errorCount}`);
|
||
console.log(` 备份位置: ${backupDir}`);
|
||
console.log('='.repeat(50));
|
||
|
||
if (errorCount > 0) {
|
||
console.log('\n⚠️ 有文件处理失败,请检查错误信息');
|
||
process.exit(1);
|
||
} else {
|
||
console.log('\n✅ 清理完成!源文件已备份到 .backup 目录');
|
||
console.log('\n💡 恢复方法:');
|
||
console.log(' 运行: node scripts/encrypt/restore.js');
|
||
console.log(' 或手动从 .backup 目录复制文件回原位置');
|
||
}
|