0
点赞
收藏
分享

微信扫一扫

前端项目一键打包自动部署2.0版本

alanwhy 2024-11-18 阅读 12
前端

流程改为:

1、先上传文件,名字跟需要上传的目录名字不同
2、删除目标文件夹
3、把之前上传的文件夹改成目标文件夹

因为上传的速度会比较慢,大概需要5-8秒,删除和改名字都是很快的,大概1秒不到就好了,所以用户感觉就是无感的

完整代码看我上一篇博客 1.0版本

const config = require('./config.js')
const shell = require('shelljs')
const path = require('path');
let Client = require('ssh2-sftp-client');
// 打包 npm run build
const compileDist = async () => {
  if(shell.exec(`npm run build`).code==0) {
    console.log("打包成功")
  }
}

async function connectSSh() {
  let sftp = new Client();
  let bfPath = config[envKey].rmpath+'bf';//提前上传的文件夹
  sftp.connect({
    host: config.ip, // 服务器 IP
    port: config.port,
    username: config.username,
    password: config.password
  }).then((data) => {
	console.log("开始上传")
	return sftp.uploadDir(path.resolve(__dirname, config[envKey].uploadFileUrl), bfPath);
  }).then(() => {
    console.log("执行删除服务器文件")
    return sftp.rmdir(config.rmpath, true);
  }).then(async () => {
    console.log("开始改名")
    await sftp.rename(bfPath, config[envKey].rmpath);config.path);
    console.log("上传完成");
    sftp.end();
  }).catch((err) => {
    console.log(err, '失败');
    sftp.end();
  });
}
async function runTask() {
  await compileDist()     //打包完成
  await connectSSh()      //提交上传
}
runTask()
举报

相关推荐

0 条评论