function jsonMerge()

in gulpfile.js [402:446]


function jsonMerge (cb) {
  // Make sure destination parent directory exists
  if (!fs.existsSync('./tmp/data/datasets/')) {
    fs.mkdirSync('./tmp/data/datasets/');
  }

  let repos = fs.readdirSync(dataSourcesDirectory).filter(function(file) {
    return fs.statSync(path.join(dataSourcesDirectory, file)).isDirectory();
  });

  // Loop over datasets and conflate Metadata
  let top = {};
  repos.forEach(function (repo) {
    // Pretty dir name for finding datasets
    var datasets = requireDir(`./tmp/data/unmerged/${repo}/datasets`);
    for (var k in datasets) {
      var dataset = datasets[k];
      const slug = generateSlug(k);
      dataset.Slug = slug;
      // If dataset (slug) already exists, only thing we're
      // copying over is Metadata
      if (top[slug]) {
        if (top[slug]['Metadata']) {
          for (var l in dataset.Metadata) {
            top[slug]['Metadata'][l] = dataset.Metadata[l];
          }
        } else {
          top[slug]['Metadata'] = dataset.Metadata;
        }
        top[slug]['Sources'].push(repo);
      } else {
        top[slug] = dataset;
        top[slug]['Sources'] = [repo];
      }
    }
  });

  // Loop over datasets and write out
  for (var k in top) {
    const dataset = top[k];
    fs.writeFileSync(`./tmp/data/datasets/${dataset.Slug}.json`, JSON.stringify(dataset));
  }

  return cb();
};