<commit_before>/* eslint no-underscore-dangle: 0 */

import fs from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';
import rimraf from 'rimraf';
import {
  isFunction,
  tryParseJson,
  tryStringifyJson,
  processPath,
  processPathNoJson,
} from './utils';

/**
 * _get - function the gets filePath and a callback and returns a parse json
 *        from the file in the specefied path
 *
 * @param  {string} filePath - path to the file relative to the `userData` folder
 * @param  {function} cb - a callback function
 */
function _get(filePath, cb) {
  const fullPath = processPath(filePath);
  return fs.readFile(fullPath, { encoding: 'utf8' }, (err, json) => {
    if (!err) {
      const object = tryParseJson(json);

      if (object instanceof Error) {
        return cb(new Error(`The file in path ${fullPath} is not a valid json file`), null);
      }

      return cb(null, object);
    }

    if (err.code === 'ENOENT') {
      return cb(new Error(`The file in path ${fullPath} doesn\'t exist`), null);
    }

    return cb(err);
  });<commit_msg>Add human readable message to existing ENOENT error<commit_after>/* eslint no-underscore-dangle: 0 */

import fs from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';
import rimraf from 'rimraf';
import {
  isFunction,
  tryParseJson,
  tryStringifyJson,
  processPath,
  processPathNoJson,
} from './utils';

/**
 * _get - function the gets filePath and a callback and returns a parse json
 *        from the file in the specefied path
 *
 * @param  {string} filePath - path to the file relative to the `userData` folder
 * @param  {function} cb - a callback function
 */
function _get(filePath, cb) {
  const fullPath = processPath(filePath);
  return fs.readFile(fullPath, { encoding: 'utf8' }, (err, json) => {
    if (!err) {
      const object = tryParseJson(json);

      if (object instanceof Error) {
        return cb(new Error(`The file in path ${fullPath} is not a valid json file`), null);
      }

      return cb(null, object);
    }

    if (err.code === 'ENOENT') {
      err.message = `The file in path ${fullPath} doesn\'t exist`;
    }

    return cb(err);
  });
{'fraction_non_alphanumeric': 0.12349676225716928, 'fraction_numerical': 0.0018501387604070306, 'mean_word_length': 3.175675675675676, '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': '5343048', 'n_tokens_mistral': 692, 'n_tokens_neox': 676, 'n_words': 243}