feather/bin/sync-algolia.js

74 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-05-18 07:51:49 +03:00
import algolia from 'algoliasearch';
import icons from '../dist/icons.json';
import tags from '../src/tags.json';
const ALGOLIA_APP_ID = '5EEOG744D0';
if (
process.env.TRAVIS_PULL_REQUEST === 'false' &&
process.env.TRAVIS_BRANCH === 'master'
) {
syncAlgolia();
} else {
2018-05-18 08:42:08 +03:00
console.log('Skipped Algolia sync.');
2018-05-18 07:51:49 +03:00
}
function syncAlgolia() {
// ALGOLIA_ADMIN_KEY must be added as an environment variable in Travis CI
2018-05-18 07:51:49 +03:00
const client = algolia(ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_KEY);
2018-05-19 03:41:39 +03:00
console.log('Initializing target and temporary indexes...');
2018-05-18 07:51:49 +03:00
const index = client.initIndex('icons');
const indexTmp = client.initIndex('icons_tmp');
2018-05-18 07:51:49 +03:00
2018-05-19 05:08:46 +03:00
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] || [],
}));
2018-05-19 05:08:46 +03:00
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);
});
}
2018-05-18 07:51:49 +03:00
2018-05-19 05:08:46 +03:00
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);
});
});
2018-05-19 05:08:46 +03:00
}
function addObjects(index, objects) {
return new Promise((resolve, reject) => {
index.addObjects(objects, (error, contents) => {
if (error) reject(error);
resolve(contents);
});
});
}
2018-05-18 07:51:49 +03:00
2018-05-19 05:08:46 +03:00
function moveIndex(client, indexNameSrc, indexNameDest) {
return new Promise((resolve, reject) => {
client.moveIndex(indexNameSrc, indexNameDest, (error, contents) => {
if (error) reject(error);
resolve(contents);
});
2018-05-18 07:51:49 +03:00
});
}