source: tanzpartie-hugo/algolia/data-upload.js @ 17759

Last change on this file since 17759 was 16971, checked in by Henrik Bettermann, 3 years ago
File size: 1.3 KB
Line 
1const argv = require("yargs/yargs")(process.argv.slice(2))
2  .boolean("c").alias("c", "clear-index").describe("c", "Clear Algolia index before upload")
3  .alias("f", "index-file").nargs("f", 1).describe("f", "Index file to upload to Algolia")
4  .alias("a", "app-id").nargs("a", 1).describe("a", "Algolia application ID")
5  .alias("k", "admin-api-key").nargs("k", 1).describe("k", "Algolia admin API key")
6  .alias("n", "index-name").nargs("n", 1).describe("n", "Algolia index name")
7  .demandOption(["f", "a", "k", "n"])
8  .help("h")
9  .alias("h", "help")
10  .argv;
11
12const algoliaSearch = require("algoliasearch");
13const client = algoliaSearch(argv["app-id"], argv["admin-api-key"]);
14const algoliaIndex = client.initIndex(argv["index-name"]);
15const jsonfile = require("jsonfile");
16
17const saveObjects = () => {
18  jsonfile.readFile(argv["index-file"], function (err, indices) {
19    if (err) {
20      console.error(err);
21    } else {
22      algoliaIndex.saveObjects(indices).then(() => {
23        console.log("Uploaded data to index %s", argv["index-name"]);
24      })
25      .catch(err => {
26        console.log(err);
27      });
28    }
29  })
30};
31
32if (argv["clear-index"]) {
33  algoliaIndex.clearObjects().then(() => {
34    console.log("Cleared data from index %s", argv["index-name"]);
35    saveObjects();
36  });
37} else {
38  saveObjects();
39}
Note: See TracBrowser for help on using the repository browser.