From 87fcac8daa05bde9a32fe10ae590641026d0c09d Mon Sep 17 00:00:00 2001 From: Francisco Moretti Date: Sun, 14 Jul 2024 17:48:39 +0100 Subject: [PATCH] getting things from cache! --- packages/cli/src/LocalNotionClient.ts | 31 +++++++++++++++++++++++++++ packages/cli/src/pull.ts | 17 ++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/LocalNotionClient.ts b/packages/cli/src/LocalNotionClient.ts index 6341e4a..d5c6d80 100644 --- a/packages/cli/src/LocalNotionClient.ts +++ b/packages/cli/src/LocalNotionClient.ts @@ -21,6 +21,7 @@ import { QueryDatabaseParameters, QueryDatabaseResponse, } from "@notionhq/client/build/src/api-endpoints" +import fs from "fs-extra" import { executeWithRateLimitAndRetries } from "./executeWithRateLimitAndRetries" import { @@ -272,6 +273,36 @@ export class LocalNotionClient extends Client { }, } + private loadDataFromJson = (filePath: string) => { + if (fs.existsSync(filePath)) { + const jsonData = fs.readFileSync(filePath, "utf8") + return JSON.parse(jsonData) + } + return undefined + } + + loadCacheFromDir = ({ + cacheDir, + }: { + // TODO: Add options to load only part of cache + cacheDir: string + }) => { + this.blocksChildrenCache = + this.loadDataFromJson(cacheDir + this.blockChildrenCacheFilename) || {} + + this.databaseChildrenCache = + this.loadDataFromJson(cacheDir + this.databaseChildrenCacheFilename) || {} + + this.pageObjectsCache = + this.loadDataFromJson(cacheDir + this.pageObjectsCacheFilename) || {} + + this.databaseObjectsCache = + this.loadDataFromJson(cacheDir + this.databaseObjectsCacheFilename) || {} + + this.blockObjectsCache = + this.loadDataFromJson(cacheDir + this.blocksObjectsCacheFilename) || {} + } + saveCacheToDir = ({ cacheDir, }: { diff --git a/packages/cli/src/pull.ts b/packages/cli/src/pull.ts index 04c0056..bae28e6 100644 --- a/packages/cli/src/pull.ts +++ b/packages/cli/src/pull.ts @@ -88,6 +88,7 @@ export async function notionPull(options: DocuNotionOptions): Promise { // TODO: HACK: until we can add the notion token to the config options.statusTag = "Published" + const CACHE_DIR = options.markdownOutputPath.replace(/\/+$/, "") + "/.cache/" const regularNotionClient = initNotionClient(options.notionToken) notionToMarkdown = new NotionToMarkdown({ notionClient: regularNotionClient }) @@ -110,6 +111,8 @@ export async function notionPull(options: DocuNotionOptions): Promise { auth: options.notionToken, }) + cachedNotionClient.loadCacheFromDir({ cacheDir: CACHE_DIR }) + updateNotionClient(cachedNotionClient) // Do a quick test to see if we can connect to the root so that we can give a better error than just a generic "could not find page" one. @@ -177,19 +180,11 @@ export async function notionPull(options: DocuNotionOptions): Promise { // Save pages to a json file await notionClient.saveCacheToDir({ - cacheDir: options.markdownOutputPath.replace(/\/+$/, "") + "/.cache/", + cacheDir: CACHE_DIR, }) - await saveDataToJson( - objectsTree, - options.markdownOutputPath.replace(/\/+$/, "") + - "/.cache/" + - "object_tree.json" - ) - await saveDataToJson( - pages, - options.markdownOutputPath.replace(/\/+$/, "") + "/.cache/" + "pages.json" - ) + await saveDataToJson(objectsTree, CACHE_DIR + "object_tree.json") + await saveDataToJson(pages, CACHE_DIR + "pages.json") info(`Found ${pages.length} pages`) endGroup()