|
@@ -1,12 +1,13 @@
|
|
|
-// const client = require("ali-oss");
|
|
|
+const client = require("ssh2-sftp-client");
|
|
|
const path = require("path");
|
|
|
const fs = require("fs");
|
|
|
+
|
|
|
const { execSync } = require("child_process");
|
|
|
|
|
|
(async function Init() {
|
|
|
const localDir = "dist/";
|
|
|
const dirPath = path.resolve(__dirname, "./" + localDir);
|
|
|
- const list = []
|
|
|
+ const list = [];
|
|
|
|
|
|
/**
|
|
|
* 获取localDir下的所有文件路径
|
|
@@ -23,6 +24,48 @@ const { execSync } = require("child_process");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ function upServer() {
|
|
|
+ const config = {
|
|
|
+ path: {
|
|
|
+ // 远程地址
|
|
|
+ romotePath: "/var/www/test",
|
|
|
+ // 本地地址
|
|
|
+ localPath: path.resolve(__dirname, "./dist"),
|
|
|
+ },
|
|
|
+ romote: {
|
|
|
+ // 服务器 ip 地址
|
|
|
+ host: "123.57.195.226",
|
|
|
+ // 端口号,默认是 22
|
|
|
+ port: 22,
|
|
|
+ // 登录的用户名
|
|
|
+ username: "root",
|
|
|
+ // 登录密码
|
|
|
+ password: "581100Li.",
|
|
|
+ },
|
|
|
+ };
|
|
|
+ const sftp = new client();
|
|
|
+ sftp
|
|
|
+ .connect(config.romote)
|
|
|
+ .then(() => {
|
|
|
+ console.log("连接成功,删除中... ");
|
|
|
+ return sftp.rmdir(config.path.romotePath, true);
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ console.log("上传中... ");
|
|
|
+ return sftp.uploadDir(config.path.localPath, config.path.romotePath);
|
|
|
+ })
|
|
|
+ .then(data => {
|
|
|
+ console.log("上传完成", data);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ console.log("出错了!!", err);
|
|
|
+ })
|
|
|
+ .finally(() => {
|
|
|
+ // 断开连接
|
|
|
+ sftp.end();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 上传git
|
|
|
*/
|
|
@@ -41,7 +84,8 @@ const { execSync } = require("child_process");
|
|
|
execFun();
|
|
|
try {
|
|
|
await getFiles(dirPath);
|
|
|
- console.log("获取文件路径---完成", list);
|
|
|
+ upServer();
|
|
|
+ console.log("获取文件路径---完成");
|
|
|
} catch (err) {
|
|
|
console.log("数据上传oss---失败");
|
|
|
}
|