Commit 179b482c authored by Cole Bemis's avatar Cole Bemis

build: Use promises in algolia script

parent a23698d5
...@@ -21,23 +21,53 @@ function syncAlgolia() { ...@@ -21,23 +21,53 @@ function syncAlgolia() {
const index = client.initIndex('icons'); const index = client.initIndex('icons');
const indexTmp = client.initIndex('icons_tmp'); const indexTmp = client.initIndex('icons_tmp');
indexTmp.setSettings({ console.log(
searchableAttributes: ['unordered(name)', 'unordered(tags)'], "Copying target index's settings, synonyms and rules into temporary index...",
customRanking: ['asc(name)'], );
}); scopedCopyIndex(client, index.indexName, indexTmp.indexName)
.then(() => {
const objects = Object.keys(icons).map(name => ({
name,
tags: tags[name] || [],
}));
const records = Object.keys(icons).map(name => ({ console.log('Adding objects to the temporary index...');
name, return addObjects(indexTmp, objects);
tags: tags[name] || [], })
})); .then(() => {
console.log('Moving temporary index to target index...');
return moveIndex(client, indexTmp.indexName, index.indexName);
});
}
console.log('Pushing data to the temporary index...'); function scopedCopyIndex(
indexTmp.addObjects(records, err => { client,
if (err) throw err; indexNameSrc,
indexNameDest,
scope = ['settings', 'synonyms', 'rules'],
) {
return new Promise((resolve, reject) => {
client.copyIndex(indexNameSrc, indexNameDest, scope, (error, contents) => {
if (error) reject(error);
resolve(contents);
});
}); });
}
function addObjects(index, objects) {
return new Promise((resolve, reject) => {
index.addObjects(objects, (error, contents) => {
if (error) reject(error);
resolve(contents);
});
});
}
console.log('Moving temporary index to target index...'); function moveIndex(client, indexNameSrc, indexNameDest) {
client.moveIndex(indexTmp.indexName, index.indexName, err => { return new Promise((resolve, reject) => {
if (err) throw err; client.moveIndex(indexNameSrc, indexNameDest, (error, contents) => {
if (error) reject(error);
resolve(contents);
});
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment