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();
|
2018-05-18 08:21:17 +03:00
|
|
|
} else {
|
2018-05-18 08:42:08 +03:00
|
|
|
console.log('Skipped Algolia sync.');
|
2018-05-18 07:51:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function syncAlgolia() {
|
2018-05-19 02:34:56 +03:00
|
|
|
// 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');
|
2018-05-19 03:23:23 +03:00
|
|
|
const indexTmp = client.initIndex('icons_tmp');
|
2018-05-18 07:51:49 +03:00
|
|
|
|
2018-05-19 04:06:19 +03:00
|
|
|
index.setSettings({
|
|
|
|
searchableAttributes: ['unordered(name)', 'unordered(tags)'],
|
|
|
|
customRanking: ['asc(name)'],
|
|
|
|
});
|
|
|
|
|
2018-05-19 03:55:24 +03:00
|
|
|
console.log('Copying target index settings into temporary index...');
|
|
|
|
client.copyIndex(index.indexName, indexTmp.indexName, ['settings'], err => {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
2018-05-19 03:23:23 +03:00
|
|
|
|
2018-05-18 07:51:49 +03:00
|
|
|
const records = Object.keys(icons).map(name => ({
|
|
|
|
name,
|
|
|
|
tags: tags[name] || [],
|
|
|
|
}));
|
|
|
|
|
2018-05-19 03:41:39 +03:00
|
|
|
console.log('Pushing data to the temporary index...');
|
|
|
|
indexTmp.addObjects(records, err => {
|
2018-05-18 07:51:49 +03:00
|
|
|
if (err) throw err;
|
2018-05-19 03:23:23 +03:00
|
|
|
});
|
2018-05-18 07:51:49 +03:00
|
|
|
|
2018-05-19 03:41:39 +03:00
|
|
|
console.log('Moving temporary index to target index...');
|
|
|
|
client.moveIndex(indexTmp.indexName, index.indexName, err => {
|
2018-05-19 03:23:23 +03:00
|
|
|
if (err) throw err;
|
2018-05-18 07:51:49 +03:00
|
|
|
});
|
|
|
|
}
|