const fs = require('fs');
const { updateIndexing } = require('./algolia-search');
const { statsPath } = require('../helpers/path');

function writeStats(allNodes) {
  return new Promise(resolve => {
    const destPath = `${statsPath}/nodes.json`;
    fs.writeFileSync(destPath, JSON.stringify(allNodes, null, 4));
    resolve();
  });
}

exports.onPostBuild = args => {
  const allNodes = args.getNodes();
  const promises = [writeStats(allNodes), updateIndexing(allNodes)];

  return Promise.all(promises);
};
