<commit_before>/* This file is a part of @mdn/browser-compat-data
 * See LICENSE file for more information. */

'use strict';

const fs = require('fs');
const { fdir } = require('fdir');

class DuplicateCompatError extends Error {
  constructor(feature) {
    super(`${feature} already exists! Remove duplicate entries.`);
    this.name = 'DuplicateCompatError';
  }
}

/**
 * Recursively load one or more directories passed as arguments.
 *
 * @param {string[]} dirs The directories to load
 * @returns {object} All of the browser compatibility data
 */
function load(...dirs) {
  let result = {};

  for (const dir of dirs) {
    const paths = new fdir()
      .withBasePath()
      .filter((fp) => fp.endsWith('.json'))
      .crawl(dir)
      .sync();

    for (const fp of paths) {
      try {
        extend(result, JSON.parse(fs.readFileSync(fp)));
      } catch (e) {
        // Skip invalid JSON. Tests will flag the problem separately.
        continue;
      }
    }
  }

  return result;
}<commit_msg>index.js: Always crawl relative to BCD root (#16324)<commit_after>/* This file is a part of @mdn/browser-compat-data
 * See LICENSE file for more information. */

'use strict';

const fs = require('fs');
const path = require('path');
const { fdir } = require('fdir');

class DuplicateCompatError extends Error {
  constructor(feature) {
    super(`${feature} already exists! Remove duplicate entries.`);
    this.name = 'DuplicateCompatError';
  }
}

/**
 * Recursively load one or more directories passed as arguments.
 *
 * @param {string[]} dirs The directories to load
 * @returns {object} All of the browser compatibility data
 */
function load(...dirs) {
  let result = {};

  for (const dir of dirs) {
    const paths = new fdir()
      .withBasePath()
      .filter((fp) => fp.endsWith('.json'))
      .crawl(path.join(__dirname, dir))
      .sync();

    for (const fp of paths) {
      try {
        extend(result, JSON.parse(fs.readFileSync(fp)));
      } catch (e) {
        // Skip invalid JSON. Tests will flag the problem separately.
        continue;
      }
    }
  }

  return result;
}
{'fraction_non_alphanumeric': 0.13556920170051961, 'fraction_numerical': 0.0023618327822390174, 'mean_word_length': 2.8579234972677594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7257673', 'n_tokens_mistral': 650, 'n_tokens_neox': 632, 'n_words': 209}