const { execSync } = require('child_process'); const { NodeSSH } = require('node-ssh'); const path = require('path'); const fs = require('fs'); const ssh = new NodeSSH(); const remoteConfig = { host: '124.221.147.173', username: 'root', password: '15703453653wxf.+', // port: 22, // 默认22端口,如有变动请取消注释 }; const localDist = path.resolve(__dirname, 'docs/.vitepress/dist'); const remoteDir = '/www/wwwroot/flys_blog'; async function main() { try { // 1. 打包 console.log('打包中...'); execSync('npx vitepress build docs', { stdio: 'inherit' }); // 2. 连接服务器 console.log('连接服务器...'); await ssh.connect(remoteConfig); // 3. 清空远程目录 console.log('清空远程目录...'); await ssh.execCommand(`rm -rf ${remoteDir}/*`); // 4. 上传 dist 文件夹内容 console.log('上传文件...'); await ssh.putDirectory(localDist, remoteDir, { recursive: true, concurrency: 10, validate: (itemPath) => !itemPath.endsWith('.DS_Store'), }); console.log('部署完成!'); ssh.dispose(); } catch (err) { console.error('部署失败:', err); process.exit(1); } } main();