build: Use promises in algolia script

This commit is contained in:
Cole Bemis 2018-05-18 19:08:46 -07:00
parent a23698d5be
commit 179b482c03

View File

@ -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 records = Object.keys(icons).map(name => ({ const objects = Object.keys(icons).map(name => ({
name, name,
tags: tags[name] || [], tags: tags[name] || [],
})); }));
console.log('Pushing data to the temporary index...'); console.log('Adding objects to the temporary index...');
indexTmp.addObjects(records, err => { return addObjects(indexTmp, objects);
if (err) throw err; })
}); .then(() => {
console.log('Moving temporary index to target index...'); console.log('Moving temporary index to target index...');
client.moveIndex(indexTmp.indexName, index.indexName, err => { return moveIndex(client, indexTmp.indexName, index.indexName);
if (err) throw err; });
}
function scopedCopyIndex(
client,
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);
});
});
}
function moveIndex(client, indexNameSrc, indexNameDest) {
return new Promise((resolve, reject) => {
client.moveIndex(indexNameSrc, indexNameDest, (error, contents) => {
if (error) reject(error);
resolve(contents);
});
}); });
} }