#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const DEMO_DIST_DIR = 'examples/worker-system-demo/dist'; const VITEPRESS_DEMO_DIR = 'docs/public/demos/worker-system'; function updateWorkerDemo() { console.log('🔄 更新 Worker System Demo 资源...'); try { // 1. 清理旧的 JS 文件 const assetsDir = path.join(VITEPRESS_DEMO_DIR, 'assets'); if (fs.existsSync(assetsDir)) { const files = fs.readdirSync(assetsDir); const jsFiles = files.filter(file => file.startsWith('index-') && file.endsWith('.js')); for (const jsFile of jsFiles) { const filePath = path.join(assetsDir, jsFile); fs.unlinkSync(filePath); console.log(`🗑️ 删除旧文件: ${jsFile}`); } } // 2. 复制新的资源文件 const sourceAssetsDir = path.join(DEMO_DIST_DIR, 'assets'); if (!fs.existsSync(sourceAssetsDir)) { throw new Error(`源目录不存在: ${sourceAssetsDir}`); } // 确保目标目录存在 if (!fs.existsSync(assetsDir)) { fs.mkdirSync(assetsDir, { recursive: true }); } const sourceFiles = fs.readdirSync(sourceAssetsDir); const newJsFile = sourceFiles.find(file => file.startsWith('index-') && file.endsWith('.js')); if (!newJsFile) { throw new Error('未找到新的 JS 文件'); } // 复制新的 JS 文件 const sourcePath = path.join(sourceAssetsDir, newJsFile); const targetPath = path.join(assetsDir, newJsFile); fs.copyFileSync(sourcePath, targetPath); console.log(`📁 复制新文件: ${newJsFile}`); // 3. 同步 HTML 结构更新 const sourceIndexPath = path.join(DEMO_DIST_DIR, 'index.html'); const targetIndexPath = path.join(VITEPRESS_DEMO_DIR, 'index.html'); if (fs.existsSync(sourceIndexPath) && fs.existsSync(targetIndexPath)) { const sourceContent = fs.readFileSync(sourceIndexPath, 'utf-8'); let targetContent = fs.readFileSync(targetIndexPath, 'utf-8'); // 提取 controls 部分 const sourceControlsMatch = sourceContent.match(/