forked from tinacms/tina.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateIndices.ts
97 lines (88 loc) · 2.33 KB
/
createIndices.ts
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
89
90
91
92
93
94
95
96
97
require('dotenv').config()
import algoliasearch, { SearchIndex } from 'algoliasearch'
import { stripMarkdown } from '../utils/blog_helpers'
import fetchDocs from '../data-api/fetchDocs'
import fetchBlogs from '../data-api/fetchBlogs'
import fetchGuides from '../data-api/fetchGuides'
const MAX_BODY_LENGTH = 200
const mapContentToIndex = async ({
content,
...obj
}: Partial<{ data: { slug: string }; content: string }>) => {
return {
...obj.data,
excerpt: await stripMarkdown((content || '').substring(0, MAX_BODY_LENGTH)),
objectID: obj.data.slug,
}
}
const saveIndex = async (client: any, indexName: string, data: any) => {
try {
const index = client.initIndex(indexName)
const result = await index.saveObjects(data)
console.log(
`${indexName}: added/updated ${result.objectIDs.length} entries`
)
const numRemoved = await cleanupIndex(index, data)
if (numRemoved > 0) {
console.log(`${indexName}: removed ${numRemoved} entries`)
}
} catch (error) {
console.log(error)
}
}
const cleanupIndex = async (index: SearchIndex, currentData: any) => {
let currentObjects: Set<string> = new Set()
let objectsToDelete: Set<string> = new Set()
let numRemoved = 0
currentData.map(item => {
currentObjects.add(item.objectID)
})
await index.browseObjects({
batch: hits => {
hits.forEach(hit => {
if (!currentObjects.has(hit.objectID)) {
objectsToDelete.add(hit.objectID)
}
})
},
})
await Promise.all(
Array.from(objectsToDelete).map(async objectID => {
await index.deleteObject(objectID)
numRemoved++
})
)
return numRemoved
}
const createIndices = async () => {
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_ADMIN_KEY
)
const docs = await fetchDocs()
await saveIndex(
client,
'Tina-Docs-Next',
await Promise.all(docs.map(mapContentToIndex))
)
const blogs = await fetchBlogs()
await saveIndex(
client,
'Tina-Blogs-Next',
await Promise.all(blogs.map(mapContentToIndex))
)
const guides = await fetchGuides()
await saveIndex(
client,
'Tina-Guides-Next',
await Promise.all(guides.map(mapContentToIndex))
)
}
createIndices()
.then(() => {
console.log('indices created')
})
.catch(e => {
console.error(e)
process.kill(1)
})