更新优化

This commit is contained in:
24kycj
2025-12-12 00:10:13 +08:00
parent 026d564c92
commit 50ff0d347a
34 changed files with 8669 additions and 7884 deletions
+116
View File
@@ -0,0 +1,116 @@
# 加密脚本使用说明
## 📁 文件说明
- `encrypt-electron.js` - 主加密脚本
- `encrypt-electron-win.bat` - Windows 批处理脚本
- `encrypt-electron.sh` - Linux/Mac Shell 脚本
- `cleanup-jsc.js` - 清理 .jsc 文件脚本
- `cleanup.js` - 清理源文件脚本(备份到 .backup)
- `restore.js` - 恢复源文件脚本(从 .backup)
- `module-map.json` - **模块文件名映射配置**(位于 `lib` 目录)
## 🔧 模块文件名映射配置
### 功能说明
`module-map.json` 允许你自定义加密后生成的文件名。这对于:
- 版本管理:不同版本使用不同的文件名
- 安全加固:使用不易猜测的文件名
- 多版本共存:同时保留多个版本的加密文件
### 配置文件结构
```json
{
"mappings": {
"design1": {
"core": "core", // 原文件名 → 加密后文件名(不含扩展名)
"output": "output",
"ui": "ui"
},
"design2": {
"core": "core",
"output": "output",
"ui": "ui"
},
"main": {
"index": "index",
"design1": "design1",
"design2": "design2"
}
}
}
```
### 使用示例
#### 示例 1:添加版本号
```json
{
"mappings": {
"design1": {
"core": "core_v2",
"output": "output_v2",
"ui": "ui_v2"
}
}
}
```
加密后会生成:`core_v2.jsc`, `output_v2.jsc`, `ui_v2.jsc`
#### 示例 2:使用随机名称
```json
{
"mappings": {
"design1": {
"core": "a1b2c3",
"output": "x9y8z7",
"ui": "m5n6o4"
}
}
}
```
加密后会生成:`a1b2c3.jsc`, `x9y8z7.jsc`, `m5n6o4.jsc`
### 使用步骤
1. **修改配置文件**
```bash
# 编辑 lib/module-map.json
# 修改 mappings 中的值
```
2. **运行加密脚本**
```bash
npm run encrypt:win # Windows
npm run encrypt:linux # Linux/Mac
```
3. **验证结果**
- 检查 `lib/design1/` 和 `lib/design2/` 目录
- 确认生成了映射后的 `.jsc` 文件
### ⚠️ 注意事项
1. **唯一性**:确保映射值唯一,避免文件名冲突
2. **重新加密**:修改配置后必须重新运行加密脚本
3. **备份**:修改前建议备份配置文件
4. **兼容性**:映射后的文件名会在运行时自动识别,无需修改代码
5. **清理**:清理脚本会自动识别映射后的文件名
### 🔄 恢复默认
如果不想使用映射,将所有值改回原文件名即可:
```json
{
"mappings": {
"design1": {
"core": "core",
"output": "output",
"ui": "ui"
}
}
}
```
+95
View File
@@ -0,0 +1,95 @@
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} 个文件不存在`);
+162
View File
@@ -0,0 +1,162 @@
/**
* 清理脚本 - 删除源 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 目录复制文件回原位置');
}
+8
View File
@@ -0,0 +1,8 @@
@echo off
chcp 65001 >nul
set ELECTRON_RUN_AS_NODE=true
set NODE_OPTIONS=--max-old-space-size=4096
cd /d "%~dp0\..\.."
electron scripts\encrypt\encrypt-electron.js
pause
+154
View File
@@ -0,0 +1,154 @@
// 设置输出编码为 UTF-8(修复 Windows 中文乱码问题)
if (process.platform === 'win32') {
process.stdout.setDefaultEncoding('utf8');
process.stderr.setDefaultEncoding('utf8');
// 设置环境变量确保中文正确显示
if (!process.env.CHCP) {
process.env.CHCP = '65001';
}
}
// 确保在 Electron 环境中运行
if (!process.versions.electron) {
console.error('❌ 错误:此脚本必须在 Electron 环境中运行!');
console.error('请使用以下方式运行:');
console.error(' Windows: encrypt-electron-win.bat');
console.error(' Linux/Mac: ./encrypt-electron.sh');
console.error(' 或: npm run encrypt:win / npm run encrypt:linux');
process.exit(1);
}
const bytenode = require('bytenode');
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'));
console.log('✅ 已加载模块映射配置\n');
} 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';
}
const filesToEncrypt = [
'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'
];
// 先删除所有旧的 .jsc 文件(包括映射后的文件名)
console.log('正在删除旧的 .jsc 文件...');
let deletedCount = 0;
filesToEncrypt.forEach(filePath => {
// 删除默认名称的 .jsc 文件(跨平台兼容)
const fullPath = path.join(projectRoot, filePath);
const defaultJscPath = fullPath + 'c';
if (fs.existsSync(defaultJscPath)) {
try {
fs.unlinkSync(defaultJscPath);
deletedCount++;
} catch (error) {
console.error(`⚠️ 删除失败: ${defaultJscPath} - ${error.message}`);
}
}
// 删除映射后的 .jsc 文件(跨平台兼容)
const dir = path.dirname(fullPath);
const encryptedFileName = getEncryptedFileName(filePath);
const mappedJscPath = path.join(dir, encryptedFileName);
// 使用 path.resolve 进行规范化比较,确保跨平台兼容
const normalizedDefault = path.resolve(defaultJscPath);
const normalizedMapped = path.resolve(mappedJscPath);
if (fs.existsSync(mappedJscPath) && normalizedMapped !== normalizedDefault) {
try {
fs.unlinkSync(mappedJscPath);
deletedCount++;
} catch (error) {
console.error(`⚠️ 删除失败: ${mappedJscPath} - ${error.message}`);
}
}
});
console.log(`已删除 ${deletedCount} 个旧的 .jsc 文件\n`);
// 开始编译
let successCount = 0, failCount = 0;
filesToEncrypt.forEach(filePath => {
try {
const fullPath = path.join(projectRoot, filePath);
if (!fs.existsSync(fullPath)) {
console.error(`❌ 文件不存在: ${filePath}`);
failCount++;
return;
}
// 编译文件(bytenode 会生成默认的 .jsc 文件)
bytenode.compileFile({ filename: fullPath });
// 检查默认的 .jsc 文件是否生成
const defaultJscPath = fullPath + 'c';
if (!fs.existsSync(defaultJscPath)) {
console.error(`❌ 编译失败: ${filePath}`);
failCount++;
return;
}
// 如果配置了映射,重命名文件(跨平台兼容)
const dir = path.dirname(fullPath);
const encryptedFileName = getEncryptedFileName(filePath);
const mappedJscPath = path.join(dir, encryptedFileName);
// 使用 path.resolve 进行规范化比较,确保跨平台兼容
const normalizedDefault = path.resolve(defaultJscPath);
const normalizedMapped = path.resolve(mappedJscPath);
if (normalizedMapped !== normalizedDefault) {
// 重命名文件
fs.renameSync(defaultJscPath, mappedJscPath);
console.log(`${filePath}${encryptedFileName}`);
} else {
console.log(`${filePath}${path.basename(defaultJscPath)}`);
}
successCount++;
} catch (error) {
console.error(`❌ 编译失败: ${filePath} - ${error.message}`);
failCount++;
}
});
console.log(`\n编译完成!成功: ${successCount}, 失败: ${failCount}`);
process.exit(failCount > 0 ? 1 : 0);
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
# 设置 UTF-8 编码环境变量,确保中文正确显示
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export ELECTRON_RUN_AS_NODE=true
export NODE_OPTIONS=--max-old-space-size=4096
# 切换到项目根目录
cd "$(dirname "$0")/../.."
# 运行加密脚本
electron scripts/encrypt/encrypt-electron.js
+126
View File
@@ -0,0 +1,126 @@
/**
* 恢复脚本 - 从备份目录恢复源 JS 文件
*
* 使用方法:
* node restore.js
*
* 此脚本会从 .backup 目录恢复所有备份的源文件
*/
const fs = require('fs');
const path = require('path');
// 获取项目根目录(脚本在 scripts/encrypt 目录下,需要向上两级)
const projectRoot = path.resolve(__dirname, '../..');
// 备份目录
const backupDir = path.join(projectRoot, '.backup');
const backupInfoPath = path.join(backupDir, 'backup-info.json');
console.log('开始恢复源文件...\n');
// 检查备份目录是否存在
if (!fs.existsSync(backupDir)) {
console.error('❌ 错误:备份目录不存在!');
console.error(` 路径: ${backupDir}`);
console.error('\n💡 提示:如果没有备份,请从 Git 仓库恢复文件');
process.exit(1);
}
// 检查备份信息文件
if (!fs.existsSync(backupInfoPath)) {
console.warn('⚠️ 警告:备份信息文件不存在,将尝试恢复所有 .js 文件\n');
}
let restoredCount = 0;
let notFoundCount = 0;
let errorCount = 0;
// 读取备份信息
let filesToRestore = [];
if (fs.existsSync(backupInfoPath)) {
try {
const backupInfo = JSON.parse(fs.readFileSync(backupInfoPath, 'utf8'));
filesToRestore = backupInfo.files || [];
console.log(`📋 从备份信息读取到 ${filesToRestore.length} 个文件\n`);
} catch (error) {
console.warn(`⚠️ 无法读取备份信息文件: ${error.message}\n`);
}
}
// 如果没有备份信息,扫描备份目录
if (filesToRestore.length === 0) {
console.log('📂 扫描备份目录...\n');
function scanDir(dir, baseDir = '') {
const files = [];
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
const relativePath = path.join(baseDir, entry.name);
if (entry.isDirectory()) {
files.push(...scanDir(fullPath, relativePath));
} else if (entry.isFile() && entry.name.endsWith('.js')) {
files.push(relativePath);
}
}
return files;
}
filesToRestore = scanDir(backupDir);
console.log(`📋 扫描到 ${filesToRestore.length} 个备份文件\n`);
}
if (filesToRestore.length === 0) {
console.error('❌ 错误:没有找到任何备份文件!');
process.exit(1);
}
// 恢复文件
filesToRestore.forEach(filePath => {
try {
const backupPath = path.join(backupDir, filePath);
const restorePath = path.join(projectRoot, filePath);
// 检查备份文件是否存在
if (!fs.existsSync(backupPath)) {
console.warn(`⚠️ 备份文件不存在: ${filePath}`);
notFoundCount++;
return;
}
// 确保目标目录存在
const restoreDir = path.dirname(restorePath);
if (!fs.existsSync(restoreDir)) {
fs.mkdirSync(restoreDir, { recursive: true });
}
// 复制文件
fs.copyFileSync(backupPath, restorePath);
console.log(`✅ 已恢复: ${filePath}`);
restoredCount++;
} catch (error) {
console.error(`❌ 恢复失败: ${filePath}`);
console.error(` 错误: ${error.message}`);
errorCount++;
}
});
console.log('\n' + '='.repeat(50));
console.log(`恢复完成!`);
console.log(` 已恢复: ${restoredCount}`);
console.log(` 未找到: ${notFoundCount}`);
console.log(` 错误: ${errorCount}`);
console.log('='.repeat(50));
if (errorCount > 0) {
console.log('\n⚠️ 有文件恢复失败,请检查错误信息');
process.exit(1);
} else {
console.log('\n✅ 恢复完成!源文件已从备份目录恢复');
console.log('\n💡 提示:');
console.log(' - 备份文件仍保留在 .backup 目录');
console.log(' - 如需删除备份,请手动删除 .backup 目录');
}