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 indexTmp = client.initIndex('icons_tmp');
indexTmp.setSettings({
searchableAttributes: ['unordered(name)', 'unordered(tags)'],
customRanking: ['asc(name)'],
});
console.log(
"Copying target index's settings, synonyms and rules into temporary index...",
);
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 => ({
name,
tags: tags[name] || [],
}));
console.log('Adding objects to the temporary index...');
return addObjects(indexTmp, objects);
})
.then(() => {
console.log('Moving temporary index to target index...');
return moveIndex(client, indexTmp.indexName, index.indexName);
});
}
console.log('Pushing data to the temporary index...');
indexTmp.addObjects(records, err => {
if (err) throw err;
});
console.log('Moving temporary index to target index...');
client.moveIndex(indexTmp.indexName, index.indexName, err => {
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);
});
});
}