Carson há 10 meses atrás
pai
commit
55d6db9033
5 ficheiros alterados com 79 adições e 19 exclusões
  1. 46 6
      .vitepress/config.mts
  2. 5 3
      components/Edit/index.vue
  3. 7 0
      package-lock.json
  4. 1 0
      package.json
  5. 20 10
      utils/s3Helper.ts

+ 46 - 6
.vitepress/config.mts

@@ -8,13 +8,29 @@ import { SVG, cleanupSVG, parseColors, runSVGO } from "@iconify/tools";
 
 import {
   S3Client,
-  PutObjectCommand,
   ListObjectsCommand,
   GetObjectCommand,
-  DeleteObjectsCommand,
 } from "@aws-sdk/client-s3";
-import { pathsToTree } from "../utils/s3Helper";
+import fs from "node:fs";
+import path from "node:path";
+import { exec } from "child_process";
 
+const DOC_BASE_PATH = "pages";
+
+// 执行 git restore 命令
+await new Promise((resolve, reject) =>
+  exec(`git restore ${DOC_BASE_PATH}`, (error, stdout, stderr) => {
+    if (error) {
+      console.error(`执行 git restore 时出错: ${error.message}`);
+      reject(stderr);
+    }
+    console.log(`git restore 输出结果: ${stdout}`);
+    resolve(stdout);
+  })
+);
+process.exit();
+
+// 从S3构建pages里面的markdown文件
 const s3 = new S3Client({
   credentials: {
     accessKeyId: process.env.VITE_AWS_S3_ACCESS_KEY_ID!,
@@ -26,15 +42,39 @@ const command = new ListObjectsCommand({
   Bucket: process.env.VITE_DOCS_LIST_BUCKET,
 });
 const { Contents: contents } = await s3.send(command);
-const docsTree = pathsToTree(contents!.map((f) => f.Key!));
-console.log(docsTree)
+console.log(contents);
 process.exit();
+await fs.emptyDir(DOC_BASE_PATH, { recursive: true });
+await Promise.all(
+  contents!.map((content) => {
+    return new Promise(async (resolve, reject) => {
+      try {
+        const command = new GetObjectCommand({
+          Bucket: process.env.VITE_DOCS_LIST_BUCKET,
+          Key: content.Key,
+          ResponseCacheControl: "no-cache",
+        });
+        const file = await s3.send(command);
+        const writePath = path.join(DOC_BASE_PATH, content.Key!);
+        const directory = path.dirname(writePath);
+        fs.mkdirSync(directory, { recursive: true });
+        fs.writeFileSync(writePath, await file.Body!.transformToString());
+        resolve(content.Key);
+      } catch (e) {
+        reject(e);
+      }
+      fs.writeFile;
+    });
+  })
+);
+
+// 构建sideBar数据
 
 // https://vitepress.dev/reference/site-config
 export default defineConfig({
   title: "CocoBlockly帮助文档",
   description: "Cococlass help documents",
-  srcDir: "pages",
+  srcDir: DOC_BASE_PATH,
   // themeConfig: {
   //   search: {
   //     provider: "local",

+ 5 - 3
components/Edit/index.vue

@@ -17,7 +17,7 @@ import AppendModal from "./AppendModal.vue";
 import type { TreeData } from "./Tree";
 import { ElNotification, type ElTree } from "element-plus";
 import { MdEditor } from "md-editor-v3";
-import { pathsToTree, getTreeLeafs } from "@/utils/s3Helper";
+import { s3ContentsToTree, getTreeLeafs } from "@/utils/s3Helper";
 import "md-editor-v3/lib/style.css";
 
 const s3 = new S3Client({
@@ -35,11 +35,13 @@ const sideLoading = ref(false);
 const loadS3DocsListObjects = async () => {
   const command = new ListObjectsCommand({ Bucket: import.meta.env.VITE_DOCS_LIST_BUCKET });
   const result = await s3.send(command);
-  return result.Contents!.map((item) => item.Key!).slice(0, 200);
+  return result.Contents!;
 };
 const loadSideBar = async () => {
   sideLoading.value = true;
-  dataSource.value = pathsToTree(await loadS3DocsListObjects());
+  dataSource.value = s3ContentsToTree(await loadS3DocsListObjects(), {}, (r, label, i, a, thisContent) => ({
+    isDir: a.length !== i + 1
+  }));
   sideLoading.value = false;
 };
 onMounted(() => {

+ 7 - 0
package-lock.json

@@ -18,6 +18,7 @@
         "@intlify/unplugin-vue-i18n": "^4.0.0",
         "@types/node": "^20.14.2",
         "@vue/compiler-sfc": "^3.4.29",
+        "child_process": "^1.0.2",
         "dotenv": "^16.4.5",
         "sass": "^1.77.5",
         "unplugin-icons": "^0.19.0",
@@ -3749,6 +3750,12 @@
         "url": "https://github.com/sponsors/fb55"
       }
     },
+    "node_modules/child_process": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmmirror.com/child_process/-/child_process-1.0.2.tgz",
+      "integrity": "sha512-Wmza/JzL0SiWz7kl6MhIKT5ceIlnFPJX+lwUGj7Clhy5MMldsSoJR0+uvRzOS5Kv45Mq7t1PoE8TsOA9bzvb6g==",
+      "dev": true
+    },
     "node_modules/chokidar": {
       "version": "3.6.0",
       "resolved": "https://registry.npmmirror.com/chokidar/-/chokidar-3.6.0.tgz",

+ 1 - 0
package.json

@@ -8,6 +8,7 @@
     "@intlify/unplugin-vue-i18n": "^4.0.0",
     "@types/node": "^20.14.2",
     "@vue/compiler-sfc": "^3.4.29",
+    "child_process": "^1.0.2",
     "dotenv": "^16.4.5",
     "sass": "^1.77.5",
     "unplugin-icons": "^0.19.0",

+ 20 - 10
utils/s3Helper.ts

@@ -1,19 +1,31 @@
 import _ from "lodash";
 
-export const pathsToTree = (paths: string[]) => {
+export const s3ContentsToTree = (
+  s3Contents,
+  props = {},
+  reducer: (...args: unknown[]) => Record<string, unknown> = () => ({})
+) => {
   let __result = [];
   let level = { __result, __prefix: "" };
+  props = Object.assign(
+    {
+      key: "key",
+      label: "label",
+      children: "children",
+    },
+    props
+  );
 
-  paths.forEach((path) => {
-    path.split("/").reduce((r, label, i, a) => {
+  s3Contents.forEach((s3Content) => {
+    s3Content.Key.split("/").reduce((r, label, i, a, s3Content) => {
       if (!r[label]) {
         const __prefix = `${r.__prefix ? r.__prefix + "/" : ""}${label}`;
         r[label] = { __result: [], __prefix };
         r.__result.push({
-          key: __prefix,
-          label,
-          isDir: a.length !== i + 1,
-          children: r[label].__result,
+          ...reducer(r, label, i, a),
+          [props.key]: __prefix,
+          [props.label]: label,
+          [props.children]: r[label].__result,
         });
       }
 
@@ -27,9 +39,7 @@ type TreeNodeLike = {
   children?: TreeNodeLike[];
 };
 
-export const getTreeLeafs = <T extends TreeNodeLike>(
-  treeNode: T
-): T[] => {
+export const getTreeLeafs = <T extends TreeNodeLike>(treeNode: T): T[] => {
   if (treeNode.children?.length) {
     return _.flatMap<T, T>(treeNode.children as T[], getTreeLeafs);
   }