<commit_before>/* eslint-disable no-param-reassign, no-console */
import cloneDeep from 'lodash/cloneDeep';
import QuizGenerator, { RegExpPresets } from 'word-quiz-generator';
import path from 'path';
import _ from 'lodash';

export function loadResources(_path) {
  const root = path.dirname(_path);
  const resources = require(_path);

  resources.materials.forEach((item) => {
    item.path = path.resolve(root, item.path);
  });
  resources.sources.forEach((item) => {
    item.path = path.resolve(root, item.path);
  });

  return resources;
}

/**
 * Build a new resources that is safe to expose to the Web.
 * @param {Object} resources an original resources JSON object.
 * @return {Object}
 */
function buildSafeResources(resources) {
  const safeResources = cloneDeep(resources);
  const presets = cloneDeep(RegExpPresets);

  safeResources.materials.forEach((item) => delete item.path);
  safeResources.sources.forEach((item) => delete item.path);
  safeResources.presets = JSON.parse(
    JSON.stringify(presets, (k, v) => _.isRegExp(v) ? v.source : v)
  );

  return safeResources;
}


const projectHome = path.resolve(__dirname, '../../');
const resourcesPath = path.resolve(projectHome, process.env.RESOURCES || 'resources.json');
const resources = loadResources(resourcesPath);
const safeResources = buildSafeResources(resources);

console.log(`The resources are loaded from ${resourcesPath}`);

export default {
  async materials(ctx) {
    ctx.body = safeResources.materials;
    ctx.status = 200;
  },

  async sources(ctx) {
    ctx.body = safeResources.sources;
    ctx.status = 200;
  },

  async presets(ctx) {
    ctx.body = safeResources.presets;
    ctx.status = 200;
  },
<commit_msg>Follow the updated ESLint rules<commit_after>/* eslint-disable no-param-reassign, no-console */
import cloneDeep from 'lodash/cloneDeep';
import QuizGenerator, { RegExpPresets } from 'word-quiz-generator';
import path from 'path';
import _ from 'lodash';

export function loadResources(_path) {
  const root = path.dirname(_path);
  const resources = require(_path);

  resources.materials.forEach((item) => {
    item.path = path.resolve(root, item.path);
  });
  resources.sources.forEach((item) => {
    item.path = path.resolve(root, item.path);
  });

  return resources;
}

/**
 * Build a new resources that is safe to expose to the Web.
 * @param {Object} resources an original resources JSON object.
 * @return {Object}
 */
function buildSafeResources(resources) {
  const safeResources = cloneDeep(resources);
  const presets = cloneDeep(RegExpPresets);

  safeResources.materials.forEach((item) => delete item.path);
  safeResources.sources.forEach((item) => delete item.path);
  safeResources.presets = JSON.parse(
    JSON.stringify(presets, (k, v) => (_.isRegExp(v) ? v.source : v))
  );

  return safeResources;
}


const projectHome = path.resolve(__dirname, '../../');
const resourcesPath = path.resolve(projectHome, process.env.RESOURCES || 'resources.json');
const resources = loadResources(resourcesPath);
const safeResources = buildSafeResources(resources);

console.log(`The resources are loaded from ${resourcesPath}`);

export default {
  async materials(ctx) {
    ctx.body = safeResources.materials;
    ctx.status = 200;
  },

  async sources(ctx) {
    ctx.body = safeResources.sources;
    ctx.status = 200;
  },

  async presets(ctx) {
    ctx.body = safeResources.presets;
    ctx.status = 200;
  },

{'fraction_non_alphanumeric': 0.13604651162790699, 'fraction_numerical': 0.005232558139534884, 'mean_word_length': 4.269525267993874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5663787', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1021, 'n_words': 254}