Skip to content

Commit

Permalink
✨ feat(workflow): add modification
Browse files Browse the repository at this point in the history
  • Loading branch information
summerscar committed Oct 27, 2024
1 parent f6ba5a3 commit 0c934a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"check": "biome check --apply <files>",
"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",
Expand Down
19 changes: 13 additions & 6 deletions scripts/generate-doc-desc.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions scripts/generate-doc-last-modification.ts
Original file line number Diff line number Diff line change
@@ -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);
})();

0 comments on commit 0c934a3

Please sign in to comment.