Vue3項(xiàng)目本地打包自動(dòng)上傳服務(wù)器

import { Client } from "ssh2";
import SftpClient from "ssh2-sftp-client";
import { fileURLToPath } from "url";
import { dirname } from "path";

const sftp = new SftpClient();

// SSH 連接配置
const sshConfig = {
  host: "xxx",
  port: 22,
  username: "xx",
  password: "xxxx",
};

// 獲取當(dāng)前模塊的 URL
const __filename = fileURLToPath(import.meta.url);
// 獲取當(dāng)前模塊的目錄路徑
const __dirname = dirname(__filename);

const localFolderPath = `${__dirname}/../dist`; // 本地 dist 文件夾路徑
const remoteFolderPath = "/home/xxx"; // 遠(yuǎn)程服務(wù)器目標(biāo)文件夾路徑

/* 下面的可以不用動(dòng) */
// 封裝 SSH 連接函數(shù)為 Promise
const connectSSH = (sshConfig) => {
  return new Promise((resolve, reject) => {
    const conn = new Client();
    conn
      .on("ready", () => {
        resolve(conn);
      })
      .connect(sshConfig);
    conn.on("error", (err) => {
      reject(err);
    });
  });
};

// 封裝連接 SSH 并執(zhí)行命令的函數(shù)為 Promise
const executeSSHCommand = (conn, command) => {
  return new Promise((resolve, reject) => {
    conn.exec(command, (err, stream) => {
      if (err) {
        conn.end();
        reject(err);
        return;
      }
      let stdout = "";
      let stderr = "";
      stream.on("data", (data) => (stdout += data.toString()));
      stream.stderr.on("data", (data) => (stderr += data.toString()));
      stream.on("close", (code, signal) => {
        // conn.end();
        if (code === 0) {
          resolve(stdout);
        } else {
          reject(stderr || `Command failed with code ${code}`);
        }
      });
    });
  });
};

// 封裝上傳文件夾函數(shù)為 Promise
const uploadFolder = async (localFolderPath, remoteFolderPath) => {
  await sftp
    .connect(sshConfig)
    .then(() => {
      console.log("正在上傳中...");
      return sftp.uploadDir(localFolderPath, remoteFolderPath);
    })
    .then(() => {
      console.log("File uploaded successfully");
      sftp.end();
    })
    .catch((err) => {
      console.error(err.message);
      sftp.end();
    });
};

// 主函數(shù)
const main = async () => {
  try {
    // 連接 SSH
    const conn = await connectSSH(sshConfig);
    console.log("SSH 連接已建立");

    // 刪除遠(yuǎn)程文件夾
    await executeSSHCommand(conn, `rm -rf ${remoteFolderPath}/*`);

    // 上傳文件夾
    await uploadFolder(localFolderPath, remoteFolderPath);

    // 關(guān)閉 SSH 連接
    conn.end();
  } catch (err) {
    console.error("發(fā)生錯(cuò)誤:", err);
    conn.end();
  }
};

// 執(zhí)行主函數(shù)
main();

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容