liyongli 2 years ago
parent
commit
ba1568628a
1 changed files with 20 additions and 16 deletions
  1. 20 16
      saveOSSAGitte.js

+ 20 - 16
saveOSSAGitte.js

@@ -44,20 +44,24 @@ const { execSync } = require("child_process");
       },
     };
     const sftp = new client();
-    try{
-        await sftp.connect(config.romote);
-        await sftp.rmdir(config.path.romotePath + '/css', true);
-        await sftp.rmdir(config.path.romotePath + '/fonts', true);
-        await sftp.rmdir(config.path.romotePath + '/js', true);
-        await sftp.delete(config.path.romotePath + '/index.html', true);
-        console.log("已清空原有文件");
-        // await sftp.uploadDir(config.path.localPath, config.path.romotePath);
-        // console.log("上传完成");
-        // 断开连接
-        sftp.end();
-        console.log("断开链接");
-    }catch(err){
-        console.log("更新失败",err);
+    try {
+      await sftp.connect(config.romote);
+      const isCss = sftp.exists(config.path.romotePath + "/css");
+      const isFonts = sftp.exists(config.path.romotePath + "/fonts");
+      const isJs = sftp.exists(config.path.romotePath + "/js");
+      const isIndexHtml = sftp.exists(config.path.romotePath + "/index.html");
+      isCss && await sftp.rmdir(config.path.romotePath + "/css", true);
+      isFonts && await sftp.rmdir(config.path.romotePath + "/fonts", true);
+      isJs && await sftp.rmdir(config.path.romotePath + "/js", true);
+      isIndexHtml && await sftp.delete(config.path.romotePath + "/index.html", true);
+      console.log("已清空原有文件");
+      await sftp.uploadDir(config.path.localPath, config.path.romotePath);
+      console.log("上传完成");
+      // 断开连接
+      sftp.end();
+      console.log("断开链接");
+    } catch (err) {
+      console.log("更新失败", err);
     }
   }
 
@@ -76,11 +80,11 @@ const { execSync } = require("child_process");
     }
   }
 
-  execFun();
   try {
+    execFun();
     await getFiles(dirPath);
-    await upServer();
     console.log("获取文件路径---完成");
+    await upServer();
   } catch (err) {
     console.log("数据上传oss---失败");
   }