vue本地打包并将构建文件推送到服务器对应目录下

第一步安装依赖

bash 复制代码
pnpm install node-ssh

第二步创建名为deploy.mjs文件

javascript 复制代码
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { NodeSSH } from 'node-ssh'

const ssh = new NodeSSH()

async function deploy() {
  console.log('打包成功进行部署流程')
  try {
    // 连接到远程服务器
    await ssh.connect({
      host: 'xx.xx.xx.xx',
      username: 'xxx',
      password: 'xxx',
    })

    console.log('服务器连接成功')

    // 获取当前文件的目录路径
    const __dirname = path.dirname(fileURLToPath(import.meta.url))
    const localPath = path.join(__dirname, 'dist') // 本地目录
    const remotePath = '/www/wwwroot/xxxx' // 远程目录

    // 确保 localPath 存在
    console.log(`本地文件路径: ${localPath}`)

    // 上传文件
    await ssh.putDirectory(localPath, remotePath, {
      recursive: true,
      concurrency: 10,
      validate: (itemPath) => {
        // 只上传文件
        return itemPath !== localPath
      },
    })

    console.log('部署成功')
  }
  catch (error) {
    console.error('Error:', error)
  }
  finally {
    // 断开连接
    ssh.dispose()
  }
}

deploy()

第三步在package.json中scripts里添加(实际用npm、pnpm、yarn都可以自己更换)

javascript 复制代码
 "deploy": "pnpm run build && node deploy.mjs"
相关推荐
ID_180079054736 分钟前
YouTube item_get_video接口认证方式:API密钥与OAuth2.0的选择与应用
服务器·windows·microsoft
wgl66652017 分钟前
Linux---基础IO!
linux·运维·服务器
Ancelin安心36 分钟前
kali-dirsearch的使用
linux·运维·服务器·python·计算机网络·web安全·网络安全
jun_bai1 小时前
python+Java的网盘程序升级版。无感知备份文档,保护数据资产利器。
运维·服务器
上海云盾-高防顾问1 小时前
筑牢网络防线:境外恶意网址与IP防范指南
服务器·网络·安全
胖者是谁2 小时前
EasyPlayerPro的使用方法
前端·javascript·css
EndingCoder2 小时前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux35282 小时前
Web集群管理实战指南:从架构到运维
运维·前端·架构
weixin_516023072 小时前
VESTA在Linux下的安装
linux·运维·服务器
沛沛老爹2 小时前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发