简单前端工程 部署

116 阅读1分钟

前端项目资源一键部署服务

首先

首先安装   shelljs和ssh2-sftp-client
 npm i shelljs ssh2-sftp-client --save-dev

然后

修改package.json增加上传命令
    "uploadServes": "node uploadServes/index.js"

新建 打包上传操作

index.js


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();
  sftp
    .connect({
      host: config.ip, // 服务器 IP
      port: config.port,  //  服务器端口
      username: config.username,  // 登陆用户名
      password: config.password, // 登陆用户密码
    })
    .then(() => {
      console.log('先执行删除服务器文件');
      //. config.rmpath。需要删除服务器文件的地址
      return sftp.rmdir(config.rmpath, true);
    })
    .then(() => {
      // 上传文件
      console.log('开始上传');
      //。config.path  上传文件到服务器的地址
      return sftp.uploadDir(path.resolve(__dirname, '../dist'), config.path);
    })
    .then(data => {
      console.log('上传完成');
      sftp.end();
    })
    .catch(err => {
      console.log(err, '失败');
      sftp.end();
    });
}
async function runTask() {
  await compileDist(); //打包完成
  await connectSSh(); //提交上传
}
runTask();

运行. npm run