liyongli 2 年之前
父节点
当前提交
46418ecebb
共有 1 个文件被更改,包括 55 次插入53 次删除
  1. 55 53
      saveOSSAGitte.js

+ 55 - 53
saveOSSAGitte.js

@@ -5,40 +5,50 @@ const path = require("path");
 const fs = require("fs");
 const oss = require("ali-oss");
 const { execSync } = require("child_process");
-
-(async function Init() {
-  const localDir = "dist/";
-  const dirPath = path.resolve(__dirname, "./" + localDir);
-  const baseOssDir = "topic/activity/";
-  const client = new oss({
-    region: "oss-cn-chengdu",
-    // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
-    accessKeyId: "LTAI4GEBqfF1GX4VwsYU2Wpg",
-    accessKeySecret: "rVIv0E1lRfXOCrAmkFTZnfgWiuv4ea",
-    bucket: "smcic-index",
-  });
+class saveOssGitte {
+  constructor() {
+    this.localDir = "dist/";
+    this.baseOssDir = "topic/activity/";
+    this.client = new oss({
+      region: "oss-cn-chengdu",
+      // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
+      accessKeyId: "LTAI4GEBqfF1GX4VwsYU2Wpg",
+      accessKeySecret: "rVIv0E1lRfXOCrAmkFTZnfgWiuv4ea",
+      bucket: "smcic-index",
+    });
+    this.execFun();
+    // this.delDir(baseOssDir);
+    // try {
+    //   this.getFiles(path.resolve(__dirname, "./" + this.localDir));
+    //   console.log("数据上传oss---完成");
+    // } catch (err) {
+    //   console.log("数据上传oss---失败");
+    // }
+  }
 
   /**
-   * 获取localDir下的所有文件路径
-   * @param {string} pathDir
+   * 上传git
    */
-  async function getFiles(pathDir) {
-    const dirList = fs.readdirSync(pathDir) || [];
-    for (let i = 0; i < dirList.length; i++) {
-      const P = [pathDir, dirList[i]];
-      const stat = fs.lstatSync(P.join("\\"));
-      if (!stat.isDirectory()) {
-        await upFileOSS(P);
-      } else getFiles(P.join("\\"));
+  async execFun() {
+    try {
+      execSync("git add .");
+      execSync(`git commit -m "${process.argv[2] || "提交"}"`);
+      execSync("git push -u origin master");
+      //   execSync("git push -u github master");
+      console.log("上传git---完成");
+    } catch (e) {
+      console.log("上传git---失败");
+      console.error(e);
     }
   }
 
   /**
    * 获得上传目录下的所有文件路径
+   * 删除oss指定路径下文件
    * @param {string} in_dirPath
    */
-  async function delDir(in_dirPath) {
-    let list = await client.list({
+  async delDir(in_dirPath) {
+    let list = await this.client.list({
       prefix: in_dirPath,
     });
     (list.objects || []).splice(0, 1);
@@ -46,7 +56,7 @@ const { execSync } = require("child_process");
     await Promise.all(
       list.objects.map(async v => {
         try {
-          await client.delete(v.name);
+          await this.client.delete(v.name);
         } catch (error) {
           error.failObjectName = v.name;
           return error;
@@ -55,18 +65,33 @@ const { execSync } = require("child_process");
     );
     console.log("oss文件清除---完成");
   }
+  /**
+   * 获取localDir下的所有文件路径
+   * @param {string} pathDir
+   */
+  async getFiles(pathDir) {
+    const dirList = fs.readdirSync(pathDir) || [];
+    for (let i = 0; i < dirList.length; i++) {
+      const P = [pathDir, dirList[i]];
+      const stat = fs.lstatSync(P.join("\\"));
+      if (!stat.isDirectory()) {
+        await upFileOSS(P);
+      } else getFiles(P.join("\\"));
+    }
+  }
+
   /**
    * 上传文件到oss
    * @param {string} filePath
    */
-  async function upFileOSS(filePath) {
+  async upFileOSS(filePath) {
     try {
       // 填写OSS文件完整路径和本地文件的完整路径。OSS文件完整路径中不能包含Bucket名称。
       // 如果本地文件的完整路径中未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
       let dir = filePath[0].split("\\");
       dir = dir.reverse()[0] + "/";
-      if (dir === localDir) dir = "";
-      await client.put(
+      if (dir === this.localDir) dir = "";
+      await this.client.put(
         baseOssDir + dir + filePath[1],
         path.normalize(filePath.join("\\"))
         // 自定义headers
@@ -77,29 +102,6 @@ const { execSync } = require("child_process");
       console.log("err--->", e);
     }
   }
+}
 
-  /**
-   * 上传git
-   */
-  function execFun() {
-    try {
-      execSync("git add .");
-      execSync(`git commit -m "${process.argv[2] || "提交"}"`);
-      execSync("git push -u origin master");
-      //   execSync("git push -u github master");
-      console.log("上传git---完成");
-    } catch (e) {
-      console.log("上传git---失败");
-      console.error(e);
-    }
-  }
-
-  execFun();
-  await delDir(baseOssDir);
-  try {
-    await getFiles(dirPath);
-    console.log("数据上传oss---完成");
-  } catch (err) {
-    console.log("数据上传oss---失败");
-  }
-})();
+new saveOssGitte();