From 48973560d15f433c0bc95907f4f2443180b9338e Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sun, 13 Oct 2024 14:20:20 -0400 Subject: [PATCH] pull latest changes from git every 10 mins Took 19 minutes --- src/lib/mdx.ts | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/lib/mdx.ts b/src/lib/mdx.ts index a207588..0aa614d 100644 --- a/src/lib/mdx.ts +++ b/src/lib/mdx.ts @@ -24,6 +24,13 @@ const DOCS_DIR: string = isGitUrl(config.contentSource) ? config.contentSource : path.join(config.contentSource.replace("{process}", process.cwd())); +const LAST_UPDATE_FILE = path.join( + os.tmpdir(), + "docs_cache", + "last_update.json" +); +const UPDATE_INTERVAL_MS: number = 10 * 60 * 1000; // 10 minutes in milliseconds + /** * Clone the Git repository if DOCS_DIR is a URL, else use the local directory. * If it's a Git URL, clone it to a cache directory and reuse it. @@ -35,15 +42,40 @@ const getDocsDirectory = async (): Promise => { // Pull the latest changes from the repo if we don't have it if (!fs.existsSync(cacheDir) || fs.readdirSync(cacheDir).length < 1) { + console.log("Fetching initial docs from Git..."); try { await simpleGit().clone(DOCS_DIR, cacheDir, { "--depth": 1 }); - } catch (error) {} + } catch (error) { + // Simply ignore this error. When cloning the repo for + // the first time, it'll sometimes error saying the dir + // is already created. + } + } else if (shouldUpdateRepo()) { + // Pull the latest changes from Git + console.log("Updating docs content from Git..."); + await simpleGit().pull(cacheDir); + fs.writeFileSync( + LAST_UPDATE_FILE, + JSON.stringify({ lastUpdate: Date.now() }), + "utf-8" + ); } return cacheDir; } return DOCS_DIR; }; +const shouldUpdateRepo = (): boolean => { + if (!fs.existsSync(LAST_UPDATE_FILE)) { + return true; + } + return ( + Date.now() - + JSON.parse(fs.readFileSync(LAST_UPDATE_FILE, "utf-8")).lastUpdate > + UPDATE_INTERVAL_MS + ); +}; + /** * Get the content to display in the docs. */