import fs from 'fs'; import path from 'path'; /** * 复制tree-kill依赖到dist目录 * 确保uTools插件运行时能够正确找到tree-kill模块 */ function copyTreeKillDeps() { const distDir = path.join(process.cwd(), 'dist'); const nodeModulesDir = path.join(process.cwd(), 'node_modules'); // 确保dist目录存在 if (!fs.existsSync(distDir)) { console.error('dist目录不存在,请先运行构建命令'); return; } // 复制tree-kill依赖 const treeKillSource = path.join(nodeModulesDir, 'tree-kill'); const treeKillDest = path.join(distDir, 'node_modules', 'tree-kill'); if (fs.existsSync(treeKillSource)) { // 创建目标目录 const destDir = path.dirname(treeKillDest); if (!fs.existsSync(destDir)) { fs.mkdirSync(destDir, { recursive: true }); } // 复制tree-kill目录 copyDirectory(treeKillSource, treeKillDest); console.log('✅ tree-kill依赖已复制到dist目录'); } else { console.error('❌ tree-kill依赖不存在,请先安装依赖'); } } /** * 递归复制目录 * @param {string} source 源目录路径 * @param {string} destination 目标目录路径 */ function copyDirectory(source, destination) { // 创建目标目录 if (!fs.existsSync(destination)) { fs.mkdirSync(destination, { recursive: true }); } // 读取源目录内容 const items = fs.readdirSync(source); items.forEach(item => { const sourcePath = path.join(source, item); const destPath = path.join(destination, item); const stat = fs.statSync(sourcePath); if (stat.isDirectory()) { // 递归复制子目录 copyDirectory(sourcePath, destPath); } else { // 复制文件 fs.copyFileSync(sourcePath, destPath); } }); } // 执行复制操作 copyTreeKillDeps();