UNPKG

ks-opdev-cli

Version:

ksodev: A CLI based on the WPS Open Platform

100 lines (82 loc) 2.88 kB
import fs from 'fs'; import path from 'path'; /** * 只处理temp_modules中的特殊包 * 让npm自动处理标准依赖 */ function postInstall() { const tempModulesPath = 'temp_modules'; const nodeModulesPath = 'node_modules'; console.log('🔄 执行postinstall: 恢复特殊依赖...'); // 检查是否在开发环境中(存在 src 目录表示是开发环境) if (fs.existsSync('src')) { console.log('ℹ️ 检测到开发环境,跳过 postinstall 处理'); return; } if (!fs.existsSync(tempModulesPath)) { console.log('ℹ️ 未发现 temp_modules,跳过 postinstall 处理'); console.log('📝 说明: 所有依赖将由npm自动安装'); return; } try { // 确保 node_modules 目录存在 if (!fs.existsSync(nodeModulesPath)) { fs.mkdirSync(nodeModulesPath, { recursive: true }); } // 获取特殊包列表 const specialPackages = fs.readdirSync(tempModulesPath, { withFileTypes: true }) .filter(dirent => dirent.isDirectory()) .map(dirent => dirent.name); if (specialPackages.length === 0) { console.log('📦 未发现特殊包'); return; } console.log(`📦 发现 ${specialPackages.length} 个特殊包:`); for (const pkg of specialPackages) { const sourcePath = path.join(tempModulesPath, pkg); const targetPath = path.join(nodeModulesPath, pkg); try { // 如果已存在,先删除(可能是npm安装的标准版本) if (fs.existsSync(targetPath)) { fs.rmSync(targetPath, { recursive: true }); console.log(` 🗑️ 移除已存在的: ${pkg}`); } // 复制特殊包 copyDir(sourcePath, targetPath); console.log(` ✅ 恢复特殊包: ${pkg}`); } catch (error) { console.error(` ❌ 恢复失败 ${pkg}:`, error.message); // 继续处理其他包,不中断整个过程 } } // 清理temp_modules目录 fs.rmSync(tempModulesPath, { recursive: true }); console.log('🗑️ 已清理 temp_modules 目录'); console.log('\n🎉 特殊依赖恢复完成!'); console.log('📝 说明: 标准依赖已由npm自动安装'); } catch (error) { console.error('❌ postinstall 失败:', error.message); console.log('💡 提示: 请检查npm安装过程是否正常'); process.exit(1); } } /** * 递归复制目录 */ function copyDir(src, dest) { if (!fs.existsSync(dest)) { fs.mkdirSync(dest, { recursive: true }); } const items = fs.readdirSync(src); for (const item of items) { const srcPath = path.join(src, item); const destPath = path.join(dest, item); const stat = fs.statSync(srcPath); if (stat.isDirectory()) { copyDir(srcPath, destPath); } else { fs.copyFileSync(srcPath, destPath); } } } postInstall();