<commit_before>import { addToOutput } from './errorHandler';

const path = require('path');
const readPkgUp = require('read-pkg-up');

/**
 * Recursively search for a package.json upwards containing given package
 * as a dependency or devDependency.
 * @param {string} fspath file system path to start searching from
 * @param {string} pkgName package's name to search for
 * @returns {string} resolved path to prettier
 */
function findPkg(fspath: string, pkgName: string): string | undefined {
    const res = readPkgUp.sync({ cwd: fspath, normalize: false });
    if (
        res.pkg &&
        ((res.pkg.dependencies && res.pkg.dependencies[pkgName]) ||
            (res.pkg.devDependencies && res.pkg.devDependencies[pkgName]))
    ) {
        return path.resolve(res.path, '..', 'node_modules/', pkgName);
    } else if (res.path) {
        return findPkg(path.resolve(path.dirname(res.path), '..'), pkgName);
    }
    return;
}

/**<commit_msg>Fix infinite recursion with top-level package.json (#241)<commit_after>import { addToOutput } from './errorHandler';

import * as path from 'path';
const readPkgUp = require('read-pkg-up');

/**
 * Recursively search for a package.json upwards containing given package
 * as a dependency or devDependency.
 * @param {string} fspath file system path to start searching from
 * @param {string} pkgName package's name to search for
 * @returns {string} resolved path to prettier
 */
function findPkg(fspath: string, pkgName: string): string | undefined {
    const res = readPkgUp.sync({ cwd: fspath, normalize: false });
    const { root } = path.parse(fspath);
    if (
        res.pkg &&
        ((res.pkg.dependencies && res.pkg.dependencies[pkgName]) ||
            (res.pkg.devDependencies && res.pkg.devDependencies[pkgName]))
    ) {
        return path.resolve(res.path, '..', 'node_modules/', pkgName);
    } else if (res.path) {
        const parent = path.resolve(path.dirname(res.path), '..');
        if (parent !== root) {
            return findPkg(parent, pkgName);
        }
    }
    return;
}

/**
{'fraction_non_alphanumeric': 0.14078374455732948, 'fraction_numerical': 0.001451378809869376, 'mean_word_length': 3.237704918032787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '64895', 'n_tokens_mistral': 644, 'n_tokens_neox': 616, 'n_words': 175}