-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(last-modified): add new workflow
- Loading branch information
1 parent
4aa4ba5
commit 06b19f0
Showing
10 changed files
with
167 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { exec, execSync } from "node:child_process"; | ||
import { readFileSync, writeFileSync } from "node:fs"; | ||
import { flattenAllDocs, insertOrUpdateFrontmatterKey } from "./list-all-docs"; | ||
|
||
(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, | ||
lastModified: timeStamp, | ||
}; | ||
}); | ||
docsWithLastModification.forEach((doc) => { | ||
const docString = readFileSync(doc.path, "utf-8"); | ||
const frontmatterKey = "last-modified"; | ||
const newDocString = insertOrUpdateFrontmatterKey( | ||
docString, | ||
frontmatterKey, | ||
doc.lastModified, | ||
); | ||
writeFileSync(doc.path, newDocString); | ||
console.log( | ||
`[generate-doc-last-modified]: ${doc.title} -> ${frontmatterKey}: ${doc.lastModified}`, | ||
); | ||
}); | ||
// console.log("[generate-doc-last-modification]: \n", docsWithLastModification); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { join } from "node:path"; | ||
import { Levels } from "@/types"; | ||
import { | ||
type FileItem, | ||
_listAllDocs as _listAllDocsByLevel, | ||
} from "@/utils/list-docs"; | ||
|
||
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 | ||
.flatMap((doc) => { | ||
if ("children" in doc) { | ||
return doc.children; | ||
} | ||
return doc; | ||
}) | ||
.map((doc) => { | ||
return { | ||
title: doc.title, | ||
path: join(process.cwd(), "mdx", level, (doc as FileItem).relativePath), | ||
} as { title: string; path: string; content?: string }; | ||
}); | ||
return flattenDocs; | ||
} | ||
|
||
export function insertOrUpdateFrontmatterKey( | ||
docString: string, | ||
frontmatterKey: string, | ||
content: string, | ||
) { | ||
const frontmatterReg = new RegExp(`${frontmatterKey}:(.*)`); | ||
const hasFrontmatterKey = docString.match(frontmatterReg)?.[1]; | ||
|
||
if (hasFrontmatterKey !== undefined) { | ||
const newDocString = docString.replace( | ||
frontmatterReg, | ||
`${frontmatterKey}: ${content}`, | ||
); | ||
return newDocString; | ||
} | ||
const newDocString = docString.replace( | ||
/---([\s\S]*?)---/, | ||
`---$1${frontmatterKey}: ${content}\n---`, | ||
); | ||
return newDocString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { config as envConfig } from "dotenv"; | ||
import OpenAI from "openai"; | ||
import type { ChatCompletionMessageParam } from "openai/resources/index.mjs"; | ||
envConfig({ path: ["./.env", "./.env.local"] }); | ||
|
||
const openai = new OpenAI({ | ||
apiKey: process.env.GPT_KEY, | ||
baseURL: process.env.GPT_URL, | ||
}); | ||
|
||
async function fetchChatCompletion(messages: ChatCompletionMessageParam[]) { | ||
const result = await openai.chat.completions.create({ | ||
model: "gpt-3.5-turbo", | ||
messages, | ||
}); | ||
console.log(`[chatGPT]: use ${result.usage?.total_tokens} tokens.`); | ||
return result.choices[0].message.content; | ||
} | ||
|
||
export { fetchChatCompletion }; |