安装node-ssh
yarn add node-ssh --dev
package.json
"scripts": {
"serve": "vue-cli-service serve",
"alpha": "vue-cli-service build --mode alpha",
"beta": "vue-cli-service build --mode beta",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"alphaDeploy": "vue-cli-service build --mode alpha&&node ./deploy.js"
},
deploy.js
const path = require("path");
const { NodeSSH } = require("node-ssh");
const ssh = new NodeSSH();
const configs = {
host: "***.***.***.***",
username: "****",
password: "***************",
port: **,
serverPath: "/***/*****/****/************",
distPath: path.resolve(__dirname, "dist/************"),
};
(function () {
console.log("开始上传");
ssh
.connect({
host: configs.host,
username: configs.username,
password: configs.password,
port: configs.port,
})
.then(function () {
console.log("ssh连接成功:", configs.host);
console.log("上传中...");
ssh
.putDirectory(configs.distPath, configs.serverPath)
.then(function () {
console.log("上传成功");
process.exit(0);
})
.catch((err) => {
console.log("上传出错:", err);
process.exit(0);
});
})
.catch((err) => {
console.log("ssh连接失败:", err);
process.exit(0);
});
})()