shenwu hai 7 meses
pai
achega
5499740b7d
Modificáronse 3 ficheiros con 79 adicións e 6 borrados
  1. 72 0
      config/oss.js
  2. 0 1
      config/sumitOss.js
  3. 7 5
      package.json

+ 72 - 0
config/oss.js

@@ -0,0 +1,72 @@
+const OSS = require('ali-oss');
+const fs = require('fs');
+const path = require('path');
+const { REACT_APP_ENV = 'dev' } = process.env;
+
+/**
+ * 打包结束后将打包内容上传至对应oss下的bucket
+ * 主要逻辑在package.json 在build命令执行后执行自义定的ossUploading 命令
+ * 1.步骤为删除当前bucket的全部文件
+ * 2.然后再上传
+ */
+let bucket = REACT_APP_ENV === 'dev' ? 'zx-web-book-distribution-test' : '-';
+
+// 创建 OSS 客户端实例
+var client = new OSS({
+  region: 'oss-cn-hangzhou',
+  accessKeyId: 'LTAI5tBcAkDx7i9PR8uvjqxN',
+  accessKeySecret: 'tWjouJKsb7ApTJLKVebxQFod6GYbOL',
+  bucket,
+});
+
+// 删除已有文件
+async function deleteAllFiles() {
+  try {
+    let result;
+    // 分页获取所有文件
+    do {
+      result = await client.list({
+        'max-keys': 1000, // 一次最多获取 1000 个文件
+      });
+
+      if (result.objects && result.objects.length) {
+        const keys = result.objects.map((item) => item.name);
+        console.log('Deleting files:', keys);
+
+        // 删除文件
+        await client.deleteMulti(keys);
+      }
+    } while (result.isTruncated); // 如果还有更多文件,继续循环
+    console.log(`已删除bucket:${bucket}内的全部文件`);
+  } catch (error) {
+    console.error(`删除bucket:${bucket}内的全部文件错误:`, error);
+  }
+}
+
+// 递归上传文件和目录
+async function uploadFilesFromDist(dirPath, ossPath) {
+  const files = fs.readdirSync(dirPath);
+
+  for (const file of files) {
+    const localFilePath = path.join(dirPath, file);
+    const ossFilePath = path.join(ossPath, file).replace(/\\/g, '/'); // 替换反斜杠为斜杠以兼容 OSS
+
+    const stats = fs.statSync(localFilePath);
+
+    if (stats.isDirectory()) {
+      // 如果是目录,递归上传
+      await uploadFilesFromDist(localFilePath, ossFilePath);
+    } else {
+      // 如果是文件,上传
+      console.log(`正在上传: ${localFilePath} 到 ${ossFilePath}`);
+      await client.put(ossFilePath, localFilePath);
+    }
+  }
+}
+
+// 主程序
+(async () => {
+  await deleteAllFiles();
+  const distPath = path.join(__dirname, '../dist');
+  await uploadFilesFromDist(distPath, ''); // OSS根目录
+})();

+ 0 - 1
config/sumitOss.js

@@ -1 +0,0 @@
-console.log("上传到oss")

+ 7 - 5
package.json

@@ -6,8 +6,10 @@
   "scripts": {
     "analyze": "cross-env ANALYZE=1 max build",
     "build": "max build",
-    "build:dev": "UMI_ENV=dev REACT_APP_ENV=dev max build ",
-    "build:pre": "UMI_ENV=pre REACT_APP_ENV=pre max build",
+    "build:dev": "cross-env UMI_ENV=dev REACT_APP_ENV=dev max build && npm run ossUploading:dev",
+    "build:pre": "cross-env UMI_ENV=pre REACT_APP_ENV=pre max build && npm run ossUploading:pro",
+    "ossUploading:dev": "cross-env UMI_ENV=dev REACT_APP_ENV=dev node ./config/oss.js ",
+    "ossUploading:pro": "cross-env UMI_ENV=pre REACT_APP_ENV=pre node ./config/oss.js ",
     "deploy": "npm run build && npm run gh-pages",
     "dev": "npm run start:dev",
     "gh-pages": "gh-pages -d dist",
@@ -34,8 +36,7 @@
     "test": "jest",
     "test:coverage": "npm run jest -- --coverage",
     "test:update": "npm run jest -- -u",
-    "tsc": "tsc --noEmit",
-    "postbuild": "node ./config/sumitOss.js"
+    "tsc": "tsc --noEmit"
   },
   "lint-staged": {
     "**/*.{js,jsx,ts,tsx}": "npm run lint-staged:js",
@@ -53,6 +54,7 @@
     "@ant-design/pro-components": "^2.6.48",
     "@umijs/route-utils": "^2.2.2",
     "ahooks": "^3.8.1",
+    "ali-oss": "^6.21.0",
     "antd": "^5.13.2",
     "antd-style": "^3.6.1",
     "classnames": "^2.5.1",
@@ -101,4 +103,4 @@
   "engines": {
     "node": ">=12.0.0"
   }
-}
+}