Skip to content

Commit

Permalink
save to cache in flocalcliente
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Jul 14, 2024
1 parent 1342e1e commit 9da0fca
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
56 changes: 56 additions & 0 deletions packages/cli/src/LocalNotionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
NotionDatabaseObjectsCache,
NotionPageObjectsCache,
} from "./notion-structures-types"
import { saveDataToJson } from "./utils"

export class LocalNotionClient extends Client {
databaseChildrenCache: DatabaseChildrenCache
Expand All @@ -39,6 +40,17 @@ export class LocalNotionClient extends Client {
blockObjectsCache: NotionBlockObjectsCache
notionClient: Client

private readonly blockChildrenCacheFilename = "block_children_cache.json"

private readonly databaseChildrenCacheFilename =
"database_children_cache.json"

private readonly pageObjectsCacheFilename = "page_objects_cache.json"

private readonly databaseObjectsCacheFilename = "database_objects_cache.json"

private readonly blocksObjectsCacheFilename = "block_objects_cache.json"

constructor({
auth,
pageObjectsCache,
Expand Down Expand Up @@ -259,4 +271,48 @@ export class LocalNotionClient extends Client {
})
},
}

saveCacheToDir = ({
cacheDir,
}: {
// TODO: Add options to save only part of cache
cacheDir: string
}) => {
const promises = []
promises.push(
saveDataToJson(
this.blocksChildrenCache,
cacheDir + this.blockChildrenCacheFilename
)
)

promises.push(
saveDataToJson(
this.databaseChildrenCache,
cacheDir + this.databaseChildrenCacheFilename
)
)

promises.push(
saveDataToJson(
this.pageObjectsCache,
cacheDir + this.pageObjectsCacheFilename
)
)

promises.push(
saveDataToJson(
this.databaseObjectsCache,
cacheDir + this.databaseObjectsCacheFilename
)
)

promises.push(
saveDataToJson(
this.blockObjectsCache,
cacheDir + this.blocksObjectsCacheFilename
)
)
return Promise.all(promises)
}
}
48 changes: 6 additions & 42 deletions packages/cli/src/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { convertInternalUrl } from "./plugins/internalLinks"
import { IDocuNotionContext } from "./plugins/pluginTypes"
import { getMarkdownForPage } from "./transform"
import { NotionBlock } from "./types"
import { convertToUUID } from "./utils"
import { convertToUUID, saveDataToJson } from "./utils"

type ImageFileNameFormat = "default" | "content-hash" | "legacy"
export type DocuNotionOptions = {
Expand Down Expand Up @@ -176,40 +176,9 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
await getPagesRecursively(options, "", rootPageUUID, 0, true)

// Save pages to a json file
await saveDataToJson(
cachedNotionClient.blocksChildrenCache,
options.markdownOutputPath.replace(/\/+$/, "") +
"/.cache/" +
"block_children_cache.json"
)

await saveDataToJson(
cachedNotionClient.databaseChildrenCache,
options.markdownOutputPath.replace(/\/+$/, "") +
"/.cache/" +
"database_children_cache.json"
)

await saveDataToJson(
cachedNotionClient.pageObjectsCache,
options.markdownOutputPath.replace(/\/+$/, "") +
"/.cache/" +
"page_objects_cache.json"
)

await saveDataToJson(
cachedNotionClient.databaseObjectsCache,
options.markdownOutputPath.replace(/\/+$/, "") +
"/.cache/" +
"database_objects_cache.json"
)

await saveDataToJson(
cachedNotionClient.blockObjectsCache,
options.markdownOutputPath.replace(/\/+$/, "") +
"/.cache/" +
"block_objects_cache.json"
)
await notionClient.saveCacheToDir({
cacheDir: options.markdownOutputPath.replace(/\/+$/, "") + "/.cache/",
})

await saveDataToJson(
objectsTree,
Expand All @@ -235,11 +204,6 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
endGroup()
}

async function saveDataToJson(data: any, filename: string) {
const json = JSON.stringify(data, null, 2)
await fs.writeFile(filename, json)
}

async function outputPages(
options: DocuNotionOptions,
config: IDocuNotionConfig,
Expand Down Expand Up @@ -463,7 +427,7 @@ function writePage(page: NotionPage, finalMarkdown: string) {
++counts.output_normally
}

let notionClient: Client
let notionClient: LocalNotionClient

async function getBlockChildren(id: string): Promise<NotionBlock[]> {
// we can only get so many responses per call, so we set this to
Expand Down Expand Up @@ -521,7 +485,7 @@ export function initNotionClient(notionToken: string): Client {
return notionClient
}

function updateNotionClient(client: Client) {
function updateNotionClient(client: LocalNotionClient) {
notionClient = client
}
async function fromPageId(
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from "fs-extra"

export function convertToUUID(str: string): string {
if (str.length !== 32) {
throw new Error("Input string must be 32 characters long")
Expand All @@ -7,3 +9,7 @@ export function convertToUUID(str: string): string {
16
)}-${str.slice(16, 20)}-${str.slice(20)}`
}
export async function saveDataToJson(data: any, filename: string) {
const json = JSON.stringify(data, null, 2)
await fs.writeFile(filename, json)
}

0 comments on commit 9da0fca

Please sign in to comment.