js directly uploads packaged files to the server

When deploying the front-end code, it needs to be uploaded to the server's folder. Usually the front-end staff will package it and send the dist.zip to the server through Ftp software. Although it is not troublesome, it can be simpler, that is, the front-end executes the script after packaging. , upload directly to the server.

upload.js:

'use strict'
// 下面三个插件是部署的时候控制台美化所用 可有可无,需要提前安装
// npm i ora
// npm i chalk
// npm i scp2
// 引入scp2
var client = require('scp2');
const ora = require('ora');
const chalk = require('chalk');
const spinner = ora(chalk.green('正在发布到测试服务器...'));
spinner.start();

client.scp('./dist/', {
    
         // 本地打包文件的位置
  "host": '', 				// 服务器的IP地址
  "port": '',            	// 服务器端口
  "username": '',       	// 用户名
  "password": '',     		// 密码
  "path": '/usr/...'
}, err =>{
    
    
  spinner.stop();
  if (!err) {
    
    
    console.log(chalk.green("项目发布完毕!"))
  } else {
    
    
    console.log("err", err)
  }
})

Configure in package.json

"scripts": {
    
    
	"build:prod": "vue-cli-service build",
    "upload": "node upload.js"
  },

The preparation work has been completed. Uploading requires two steps, packaging and uploading. Now start uploading. Execute the following two commands in the terminal. You can also use yarn, pnpm, cnpm, etc.:

  1. npm run build:prod
  2. npm run upload

upload completed

PS: If it is not packaged and has been packaged before, then the code uploaded to the server is the old code, not the latest one.

Guess you like

Origin blog.csdn.net/weixin_44019553/article/details/134669494