/** * 生产服务器 - DrPlayer Dashboard */ import Fastify from 'fastify'; import fastifyStatic from '@fastify/static'; import {addSPARoutes} from './fastify-spa-routes.js'; import path from 'path'; import {fileURLToPath} from 'url'; import fs from 'fs/promises'; import {execSync} from 'child_process'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // 构建和部署函数 async function buildAndDeploy() { console.log('🔨 开始构建应用...'); try { // 执行构建命令 execSync('yarn build:fastify', { stdio: 'inherit', cwd: __dirname }); console.log('✅ 构建完成'); // 确保apps目录存在 const appsDir = path.join(__dirname, 'apps'); const drplayerDir = path.join(appsDir, 'drplayer'); await fs.mkdir(drplayerDir, { recursive: true }); console.log('📁 创建apps目录'); // 复制dist内容到apps/drplayer const distDir = path.join(__dirname, 'dist'); await copyDirectory(distDir, drplayerDir); console.log('📋 复制文件到apps/drplayer'); } catch (error) { console.error('❌ 构建失败:', error.message); process.exit(1); } } // 递归复制目录 async function copyDirectory(src, dest) { await fs.mkdir(dest, { recursive: true }); const entries = await fs.readdir(src, { withFileTypes: true }); for (const entry of entries) { const srcPath = path.join(src, entry.name); const destPath = path.join(dest, entry.name); if (entry.isDirectory()) { await copyDirectory(srcPath, destPath); } else { await fs.copyFile(srcPath, destPath); } } } const fastify = Fastify({ logger: true }); // 配置选项 const PORT = 8008; const options = { appsDir: path.join(__dirname, 'apps'), port: PORT }; // 注册静态文件服务 await fastify.register(fastifyStatic, { root: options.appsDir, prefix: '/apps/', decorateReply: false, }); // 注册SPA路由支持 await fastify.register(addSPARoutes, { appsDir: options.appsDir, spaApps: ['drplayer'] }); // 根路径 - 显示应用列表 fastify.get('/', async (request, reply) => { // 读取package.json获取版本信息 let version = '1.0.0'; try { const packageJsonPath = path.join(__dirname, 'package.json'); const packageJsonContent = await fs.readFile(packageJsonPath, 'utf8'); const packageJson = JSON.parse(packageJsonContent); version = packageJson.version || '1.0.0'; } catch (error) { console.warn('无法读取package.json版本信息:', error.message); } const html = `