ZaiZai hace 2 años
padre
commit
ecb76e52bd
Se han modificado 6 ficheros con 82 adiciones y 7 borrados
  1. 2 0
      .gitignore
  2. 2 1
      package.json
  3. 64 0
      public/backup.js
  4. 1 1
      public/version.json
  5. 3 3
      scripts/build.js
  6. 10 2
      scripts/build.sh

+ 2 - 0
.gitignore

@@ -6,3 +6,5 @@ node_modules
 
 dist
 zip
+
+public/version.json

+ 2 - 1
package.json

@@ -8,7 +8,8 @@
         "build:zip": "sh ./scripts/build.sh all",
         "build:zip:wgt": "sh ./scripts/build.sh wgt",
         "build:test": "sh ./scripts/build.sh all test",
-        "build:test:wgt": "sh ./scripts/build.sh wgt test"
+        "build:test:wgt": "sh ./scripts/build.sh wgt test",
+        "test": "node ./public/backup.js"
     },
     "dependencies": {
         "axios": "^1.4.0",

+ 64 - 0
public/backup.js

@@ -0,0 +1,64 @@
+const path = require('path');
+const fs = require('fs');
+
+// 获取当前命令行上下文路径
+const currentDirectory = process.cwd();
+console.log(`----------------------------`)
+
+const backupPath = path.join(currentDirectory, '/backup/')
+
+//获取文件列表
+const files = fs.readdirSync(backupPath);
+if (files.length > 5) {
+    //遍历文件列表
+    console.log(`准备清理备份文件...`)
+    let newfiles = []
+    files.forEach((file) => {
+        const time = file.split('_');
+        if (time.length >= 2) {
+            newfiles.push({time: time[0], file: file})
+        }
+    })
+    //文件列表排序
+    newfiles = arrKeySort(newfiles, 'time', 'desc');
+    //移除文件
+    newfiles.forEach((item, index) => {
+        if (index >= 5) {
+            fs.unlinkSync(backupPath + item.file);
+            console.log(`已清理${item.file}`)
+        }
+    })
+    console.log('---------- 清理完成 ----------')
+} else {
+    console.log(`备份文件小于5份,不执行清理操作...`)
+}
+
+
+//数组对象排序
+function arrKeySort(arr, field = 'id', order = 'asc')
+{
+    return arr.sort(arrCompare(field, order));
+}
+
+function arrCompare(key, order = 'asc')
+{
+    return function innerSort(a, b) {
+        if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
+            // 该属性在任何一个对象上都不存在
+            return 0;
+        }
+        const varA = (typeof a[key] === 'string')
+            ? a[key].toUpperCase() : a[key];
+        const varB = (typeof b[key] === 'string')
+            ? b[key].toUpperCase() : b[key];
+        let comparison = 0;
+        if (varA > varB) {
+            comparison = 1;
+        } else if (varA < varB) {
+            comparison = -1;
+        }
+        return (
+            (order === 'desc') ? (comparison * -1) : comparison
+        );
+    };
+}

+ 1 - 1
public/version.json

@@ -1,3 +1,3 @@
 {
-  "value": "20230706095744"
+  "value": "20230706104556"
 }

+ 3 - 3
scripts/build.js

@@ -11,11 +11,11 @@ console.log(`更新版本更新信息...`)
 const versionPath = path.join(currentDirectory, '/public/version.json');
 const versionContent = fs.readFileSync(versionPath, 'utf8');
 //修改配置文件
-const versionJson = JSON.parse(versionContent);
-versionJson.value = dateFormat(new Date());         //版本号
+//const versionJson = JSON.parse(versionContent);
+//versionJson.value = dateFormat(new Date());         //版本号
 
 //更新版本更新信息
-fs.writeFileSync(versionPath, JSON.stringify(versionJson, null, 2));
+//fs.writeFileSync(versionPath, JSON.stringify(versionJson, null, 2));
 
 console.log(`----------------------------`)
 

+ 10 - 2
scripts/build.sh

@@ -57,9 +57,17 @@ function testServer() {
     expect "*]#"
     send "unzip -o ${file_name}\r"
 
-    # 备份当前文件
+    # 备份上一次的压缩文件
     expect "*]#"
-    send "cp ./${file_name} ./backup/${current_time}_${file_name}\r"
+    send "mv ./backup/${file_name} ./backup/${current_time}_${file_name}\r"
+
+    # 执行清理备份文件脚本
+    expect "*]#"
+    send "node ./backup.js\r"
+
+    # 移动当前文件到备份目录
+    expect "*]#"
+    send "mv ./${file_name} ./backup/${file_name}\r"
 
     # 退出
     send "exit\r"