-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathsync-orama.mjs
88 lines (75 loc) · 2.07 KB
/
sync-orama.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { readFileSync } from "node:fs";
import { globSync } from "glob";
import { generalPurposeCrawler } from "@orama/crawly";
import "dotenv/config";
const ORAMA_PRIVATE_API_KEY = process.env.ORAMA_PRIVATE_API_KEY;
const ORAMA_PRIVATE_INDEX_ID = process.env.ORAMA_PRIVATE_INDEX_ID;
const baseURL = new URL("../dist", import.meta.url).pathname;
const HTMLFiles = globSync("**/*.html", { cwd: baseURL });
const pagesToIndex = HTMLFiles.flatMap((file) => {
const path = file.replace(/\.html$/, "");
const pageContent = readFileSync(
new URL(`../dist/${file}`, import.meta.url),
"utf8"
);
const productionDocsURL = `https://docs.solidjs.com/${path}`;
return {
...generalPurposeCrawler(productionDocsURL, pageContent, {
parseCodeBlocks: false,
})[0],
contentWithCode: generalPurposeCrawler(productionDocsURL, pageContent)?.[0]
?.content,
};
});
async function emptyIndex() {
await fetch(
`https://api.oramasearch.com/api/v1/webhooks/${ORAMA_PRIVATE_INDEX_ID}/snapshot`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
authorization: `Bearer ${ORAMA_PRIVATE_API_KEY}`,
},
body: JSON.stringify([]),
}
);
}
async function upsertFreshData() {
const batches = [];
const batchesSize = 25;
for (let i = 0; i < pagesToIndex.length; i += batchesSize) {
const batch = pagesToIndex.slice(i, i + batchesSize);
batches.push(batch);
}
for (let i = 0; i < batches.length; i++) {
const batch = batches[i];
await fetch(
`https://api.oramasearch.com/api/v1/webhooks/${ORAMA_PRIVATE_INDEX_ID}/notify`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
authorization: `Bearer ${ORAMA_PRIVATE_API_KEY}`,
},
body: JSON.stringify({
upsert: batch,
}),
}
);
}
}
async function deployIndex() {
await fetch(
`https://api.oramasearch.com/api/v1/webhooks/${ORAMA_PRIVATE_INDEX_ID}/deploy`,
{
method: "POST",
headers: {
authorization: `Bearer ${ORAMA_PRIVATE_API_KEY}`,
},
}
);
console.log("Index deployed");
}
await emptyIndex();
await upsertFreshData();
await deployIndex();