#!/usr/bin/env node import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; // 获取当前脚本的目录 const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // 源目录和目标目录 const sourceDir = path.resolve(__dirname, '../.vitepress/dist'); const targetDir = path.resolve(process.cwd(), 'dist'); // 检查源目录是否存在 if (!fs.existsSync(sourceDir)) { console.error(`错误:源目录 "${sourceDir}" 不存在!`); console.error('请确保已经运行了 vitepress build 命令'); process.exit(1); } // 如果目标目录已存在,则先删除 if (fs.existsSync(targetDir)) { console.log(`目标目录 "${targetDir}" 已存在,正在删除...`); fs.rmSync(targetDir, { recursive: true, force: true }); } // 递归复制目录函数 function copyDirRecursive(src, dest) { // 创建目标目录 fs.mkdirSync(dest, { recursive: true }); // 读取源目录中的所有文件/目录 const entries = fs.readdirSync(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()) { // 递归复制子目录 copyDirRecursive(srcPath, destPath); } else { // 复制文件 fs.copyFileSync(srcPath, destPath); } } } try { // 复制目录 copyDirRecursive(sourceDir, targetDir); console.log(`✅ 成功将 ".vitepress/dist" 复制到 "${targetDir}"`); } catch (error) { console.error(`❌ 复制过程中发生错误:${error.message}`); process.exit(1); }