From 0c934a38b552f153aab4a44dc02b52362d14f7b5 Mon Sep 17 00:00:00 2001 From: summerscar Date: Sun, 27 Oct 2024 12:05:22 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(workflow):=20add=20modificatio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...generate-doc-desc.yml => generate-doc.yml} | 6 +++--- package.json | 3 ++- scripts/generate-doc-desc.ts | 19 +++++++++++++------ scripts/generate-doc-last-modification.ts | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 10 deletions(-) rename .github/workflows/{generate-doc-desc.yml => generate-doc.yml} (81%) create mode 100644 scripts/generate-doc-last-modification.ts diff --git a/.github/workflows/generate-doc-desc.yml b/.github/workflows/generate-doc.yml similarity index 81% rename from .github/workflows/generate-doc-desc.yml rename to .github/workflows/generate-doc.yml index e4db9e2..6e61b84 100644 --- a/.github/workflows/generate-doc-desc.yml +++ b/.github/workflows/generate-doc.yml @@ -1,5 +1,5 @@ -name: Generate doc description -run-name: Generate doc description +name: Generate doc +run-name: Generate doc on: [push] jobs: update-doc: @@ -14,7 +14,7 @@ jobs: - name: Run script:docs run: | npm install - npm run script:docs + npm run script:docs-desc - uses: stefanzweifel/git-auto-commit-action@v4 with: diff --git a/package.json b/package.json index 85746e4..db4be0a 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "check": "biome check --apply ", "prepare": "husky", "lint-staged": "lint-staged", - "script:docs": "tsx scripts/generate-doc-desc.ts" + "script:docs-desc": "tsx scripts/generate-doc-desc.ts", + "script:docs-modify": "tsx scripts/generate-doc-last-modification.ts" }, "dependencies": { "@keystone-6/auth": "^8.0.0", diff --git a/scripts/generate-doc-desc.ts b/scripts/generate-doc-desc.ts index 54d58b2..23b6228 100644 --- a/scripts/generate-doc-desc.ts +++ b/scripts/generate-doc-desc.ts @@ -1,5 +1,6 @@ import { readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; +import { Levels } from "@/types"; import { type FileItem, _listAllDocs as _listAllDocsByLevel, @@ -10,10 +11,8 @@ envConfig({ path: ["./.env", "./.env.local"] }); (async () => { const DESC_MIN_LENGTH = 10; - const levels = ["beginner", "intermediate"]; - const docs = ( - await Promise.all(levels.map((level) => listAllDocsByLevel(level))) - ).flat(); + + const docs = await flattenAllDocs(); // console.log("[generate-doc-desc]: \n", docs); // 筛序出文档中的 frontmatter 的 description 部分少于 DESC_MIN_LENGTH 个字的 const docsNeedToGenerateDescription = docs.filter((doc) => { @@ -33,6 +32,7 @@ envConfig({ path: ["./.env", "./.env.local"] }); ); await Promise.all( docsNeedToGenerateDescription.map(async (doc) => { + if (doc.content === undefined) return; const description = await fetchChatCompletion(doc.content); if (!description) return; console.log("[generate-doc-desc][title][", doc.title, "]: ", description); @@ -48,6 +48,14 @@ envConfig({ path: ["./.env", "./.env.local"] }); console.log("[generate-doc-desc][all]: success!"); })(); +export async function flattenAllDocs() { + const levels = [Levels.Beginner, Levels.Intermediate]; + + return ( + await Promise.all(levels.map((level) => listAllDocsByLevel(level))) + ).flat(); +} + async function listAllDocsByLevel(level: string) { const docs = await _listAllDocsByLevel(level); const flattenDocs = docs @@ -61,8 +69,7 @@ async function listAllDocsByLevel(level: string) { return { title: doc.title, path: join(process.cwd(), "mdx", level, (doc as FileItem).relativePath), - content: "", - }; + } as { title: string; path: string; content?: string }; }); return flattenDocs; } diff --git a/scripts/generate-doc-last-modification.ts b/scripts/generate-doc-last-modification.ts new file mode 100644 index 0000000..5327865 --- /dev/null +++ b/scripts/generate-doc-last-modification.ts @@ -0,0 +1,16 @@ +import { exec, execSync } from "node:child_process"; +import { flattenAllDocs } from "./generate-doc-desc"; + +(async () => { + const docs = await flattenAllDocs(); + const docsWithLastModification = docs.map((doc) => { + const timeStamp = execSync( + `git log -1 --pretty="format:%ct" -- "${doc.path}"`, + ).toString("utf-8"); + return { + ...doc, + lastModification: timeStamp, + }; + }); + console.log("[generate-doc-last-modification]: \n", docsWithLastModification); +})();