Files
eSIM-Tools/scripts/build-static.js
Abner 4a0fcc94fe feat: 重构Netlify Functions架构并增强代码质量
- 新增统一的中间件模块,提供鉴权、CORS和错误处理功能
- 重构所有Functions使用withAuth中间件简化代码结构
- 添加安全存储模块替代localStorage,防御XSS攻击
- 引入HTML清理工具,自动转义特殊字符和验证URL安全性
- 创建代码质量检查脚本,验证语法、环境变量和依赖完整性
- 添加构建日志工具和重构脚本,统一替换console.log为Logger
- 引入ESLint配置,提升代码质量和一致性
- 重构Giffgaff相关API,统一错误处理和验证逻辑
- 优化构建脚本,添加压缩和图片优化功能
- 新增健康检查端点,用于服务监控和状态报告
2025-11-23 22:20:02 +08:00

80 lines
2.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
const BuildLogger = require('./logger.js');
const fs = require('fs');
const path = require('path');
const projectRoot = path.join(__dirname, '..');
const distDir = path.join(projectRoot, 'dist');
const entries = [
'index.html',
'manifest.webmanifest',
'src'
];
async function removeDist() {
await fs.promises.rm(distDir, { recursive: true, force: true });
}
async function copyEntry(entry) {
const from = path.join(projectRoot, entry);
const to = path.join(distDir, entry);
if (!fs.existsSync(from)) {
console.warn(`⚠️ 跳过不存在的入口: ${entry}`);
return;
}
try {
const stats = await fs.promises.stat(from);
if (stats.isDirectory()) {
await copyDirectory(from, to);
} else {
await fs.promises.mkdir(path.dirname(to), { recursive: true });
await fs.promises.copyFile(from, to);
}
} catch (err) {
console.error(`❌ 复制失败 ${entry}:`, err.message);
throw err;
}
}
async function copyDirectory(source, destination) {
try {
await fs.promises.mkdir(destination, { recursive: true });
const entries = await fs.promises.readdir(source, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(source, entry.name);
const destPath = path.join(destination, entry.name);
try {
if (entry.isDirectory()) {
await copyDirectory(srcPath, destPath);
} else if (entry.isFile()) {
await fs.promises.copyFile(srcPath, destPath);
}
} catch (fileErr) {
console.warn(`⚠️ 跳过文件 ${entry.name}:`, fileErr.message);
// 继续处理其他文件
}
}
} catch (err) {
throw new Error(`复制目录失败 ${source}: ${err.message}`);
}
}
(async () => {
BuildLogger.log('🧹 清理 dist 目录...');
await removeDist();
await fs.promises.mkdir(distDir, { recursive: true });
for (const entry of entries) {
BuildLogger.log(`📦 复制 ${entry} -> dist/${entry}`);
await copyEntry(entry);
}
BuildLogger.success(' 静态资源构建完成,输出目录 dist/');
})().catch(err => {
console.error('构建静态资源失败:', err);
process.exitCode = 1;
});