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 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'],
console.log('Moving temporary index to target index...'); ) {
client.moveIndex(indexTmp.indexName, index.indexName, err => { return new Promise((resolve, reject) => {
if (err) throw err; 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);
});
}); });
} }