Skip to content

Commit 179b482

Browse files
committed
build: Use promises in algolia script
1 parent a23698d commit 179b482

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

bin/sync-algolia.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,53 @@ function syncAlgolia() {
2121
const index = client.initIndex('icons');
2222
const indexTmp = client.initIndex('icons_tmp');
2323

24-
indexTmp.setSettings({
25-
searchableAttributes: ['unordered(name)', 'unordered(tags)'],
26-
customRanking: ['asc(name)'],
27-
});
24+
console.log(
25+
"Copying target index's settings, synonyms and rules into temporary index...",
26+
);
27+
scopedCopyIndex(client, index.indexName, indexTmp.indexName)
28+
.then(() => {
29+
const objects = Object.keys(icons).map(name => ({
30+
name,
31+
tags: tags[name] || [],
32+
}));
2833

29-
const records = Object.keys(icons).map(name => ({
30-
name,
31-
tags: tags[name] || [],
32-
}));
34+
console.log('Adding objects to the temporary index...');
35+
return addObjects(indexTmp, objects);
36+
})
37+
.then(() => {
38+
console.log('Moving temporary index to target index...');
39+
return moveIndex(client, indexTmp.indexName, index.indexName);
40+
});
41+
}
3342

34-
console.log('Pushing data to the temporary index...');
35-
indexTmp.addObjects(records, err => {
36-
if (err) throw err;
43+
function scopedCopyIndex(
44+
client,
45+
indexNameSrc,
46+
indexNameDest,
47+
scope = ['settings', 'synonyms', 'rules'],
48+
) {
49+
return new Promise((resolve, reject) => {
50+
client.copyIndex(indexNameSrc, indexNameDest, scope, (error, contents) => {
51+
if (error) reject(error);
52+
resolve(contents);
53+
});
3754
});
55+
}
56+
57+
function addObjects(index, objects) {
58+
return new Promise((resolve, reject) => {
59+
index.addObjects(objects, (error, contents) => {
60+
if (error) reject(error);
61+
resolve(contents);
62+
});
63+
});
64+
}
3865

39-
console.log('Moving temporary index to target index...');
40-
client.moveIndex(indexTmp.indexName, index.indexName, err => {
41-
if (err) throw err;
66+
function moveIndex(client, indexNameSrc, indexNameDest) {
67+
return new Promise((resolve, reject) => {
68+
client.moveIndex(indexNameSrc, indexNameDest, (error, contents) => {
69+
if (error) reject(error);
70+
resolve(contents);
71+
});
4272
});
4373
}

0 commit comments

Comments
 (0)