一、安装依赖
npm i scp2
npm i ora@3.4.0
npm i ssh2
npm i readline
二、在跟目录创建文件 deploy.js,引入以下代码
const scpClient = require('scp2')
const ora = require('ora')
const loading = ora('正在部署中');
const Client = require('ssh2').Client;
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const questions = [ 'Please input server username: ', 'Please input server password: ']
const linelimit = 2
let inputArr = []
let index = 0
let server = {}
function runQueLoop() {
if (index === linelimit) {
server["username"] = inputArr[0]
server["password"] = inputArr[1]
deployFile()
return
}
rl.question(questions[index], as => {
inputArr[index] = as
index++
runQueLoop()
})
}
function deployFile() {
const service = {
host: 'xxx',
port: 22,
username:server.username,
password: server.password,
path: '/www/wwwroot/8098'
}
var conn = new Client();
conn.on('ready', function () {
conn.exec('rm -rf /home/ruoyi/nginx/html/ruoyi-ui/*', function (err, stream) {
if (err) throw err;
stream.on('close', function (code, signal) {
loading.start();
scpClient.scp(
'./dist', service,
function (err) {
loading.stop();
if (err) {
console.log('发布失败!');
throw err;
} else {
console.log('成功发布!');
}
}
);
conn.end();
}).on('data', function (data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function (data) {
console.log('STDERR: ' + data);
});
});
}).connect(service);
}
runQueLoop()
三、添加运行命令
"deploy": "npm run build:product && node ./deploy.js"
四、在控制台运行
npm run deploy