This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
forked from codemagic-ci-cd/codemagic-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algolia.js
53 lines (43 loc) · 1.77 KB
/
algolia.js
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
const algoliasearch = require('algoliasearch')
const fs = require('fs')
const apiKey = process.argv[2]
const client = algoliasearch('27CIRMYZIB', apiKey)
const index = client.initIndex('codemagic_docs')
const newObjects = JSON.parse(fs.readFileSync('./public/index.json'))
.map((newObject) => ({
...newObject,
objectID: newObject.uri,
}))
.filter(({ uri }) => uri !== '/404/' && !uri.startsWith('/partials/'))
const findObjectById = (objects, target) => objects.find(({ objectID }) => objectID === target)
const getChangedObjects = (newObjects, oldObjects) =>
newObjects
.map((newObject) => {
const oldObject = findObjectById(oldObjects, newObject.objectID)
if (!oldObject) return newObject
const differentAttributes = Object.entries(newObject).filter(([key, value]) => value !== oldObject[key])
return differentAttributes.length
? {
objectID: newObject.objectID,
...Object.fromEntries(differentAttributes),
}
: false
})
.filter(Boolean)
let oldObjects = []
index
.browseObjects({
query: '',
batch: (batch) => {
oldObjects = oldObjects.concat(batch)
},
})
.then(() => {
const objectIDsToDelete = oldObjects
.map(({ objectID }) => objectID)
.filter((oldObjectId) => !findObjectById(newObjects, oldObjectId))
const changes = getChangedObjects(newObjects, oldObjects)
index.deleteObjects(objectIDsToDelete).catch((error) => console.error(error))
index.partialUpdateObjects(changes, { createIfNotExists: true }).catch((error) => console.error(error))
})
.catch((error) => console.error(error))