<commit_before>'use strict';
/**
 * Handles reading, writing and renaming of all local files.
 * @type {exports|module.exports}
 */

const fs = require('fs');
const path = require('path');
const Maybe = require('maybe');

var rc = {};

/**
 * Enum for available local files.
 */
const FileType = new Enum(['Template', 'Rendered']);

/**
 * Reads the file contents if it exists.
 * @param folder Folder from /
 * @param file File from /$folder/
 * @param fileType Available local files (-> FileType Enum)
 * @returns {Maybe} File content or Maybe.Nothing
 */
function readFile(folder, file, fileType) {
  // Register all available File Path builder
  const pathBuilder = {
    [FileType.Template]: rc.buildTemplateFilePath,
    [FileType.Rendered]: rc.buildRenderedFilePath,
  };

  // Build full path to every file
  const fullPath = pathBuilder[fileType](folder, file);
  if (rc.exists(fullPath))
    return Maybe(fs.readFileSync(fullPath, 'utf8'));
  return Maybe();
}

/**
 * Checks if a file exists and can be read from and written to.
 * Calls the callback with an error instance, if anything went wrong.
 * @param file File to check
 * @returns boolean True if file exists, false if it doesn't (or we can't access it)
 */
rc.exists =
  file => {
    try {
      fs.accessSync(file, fs.R_OK | fs.W_OK);
      return true;
    }
    catch (err) {
      return false;
    }
  };

/**
 * Get stringified timestamp, using only numeric timestamp to stay alphanumeric
 * @param folder
 * @param file
 * @returns number
 */
rc.getFileTimeStamp =
  (folder, file) => fs.statSync(this.buildRenderedFilePath(folder, file)).mtime.getTime();

/**
 * Writes the contents in data into folder/file.
 * @param folder
 * @param file
 * @param data
 */
rc.writeRenderedFile =
  (folder, file, data) => {
    const fullPath = this.buildRenderedFilePath(folder, file);
    if (rc.exists(fullPath))
      fs.renameSync(fullPath,
        this.buildRenderedFilePath(folder, file + '_' + rc.getFileTimeStamp(folder, file))
      );
    fs.writeFileSync(fullPath, data);
  };

/**
 * Reads the file $folder/$file and returns its contents,
 * if it doesn't exist the return value is Maybe.Nothing
 * @param folder
 * @param file
 * @returns Maybe(String)
 */
rc.readRenderedFile =<commit_msg>fixes scope bug in renderer<commit_after>'use strict';
/**
 * Handles reading, writing and renaming of all local files.
 * @type {exports|module.exports}
 */

const fs = require('fs');
const path = require('path');
const Maybe = require('maybe');

var rc = {};

/**
 * Enum for available local files.
 */
const FileType = new Enum(['Template', 'Rendered']);

/**
 * Reads the file contents if it exists.
 * @param folder Folder from /
 * @param file File from /$folder/
 * @param fileType Available local files (-> FileType Enum)
 * @returns {Maybe} File content or Maybe.Nothing
 */
function readFile(folder, file, fileType) {
  // Register all available File Path builder
  const pathBuilder = {
    [FileType.Template]: rc.buildTemplateFilePath,
    [FileType.Rendered]: rc.buildRenderedFilePath,
  };

  // Build full path to every file
  const fullPath = pathBuilder[fileType](folder, file);
  if (rc.exists(fullPath))
    return Maybe(fs.readFileSync(fullPath, 'utf8'));
  return Maybe();
}

/**
 * Checks if a file exists and can be read from and written to.
 * Calls the callback with an error instance, if anything went wrong.
 * @param file File to check
 * @returns boolean True if file exists, false if it doesn't (or we can't access it)
 */
rc.exists =
  file => {
    try {
      fs.accessSync(file, fs.R_OK | fs.W_OK);
      return true;
    }
    catch (err) {
      return false;
    }
  };

/**
 * Get stringified timestamp, using only numeric timestamp to stay alphanumeric
 * @param folder
 * @param file
 * @returns number
 */
rc.getFileTimeStamp =
  (folder, file) => fs.statSync(this.buildRenderedFilePath(folder, file)).mtime.getTime();

/**
 * Writes the contents in data into folder/file.
 * @param folder
 * @param file
 * @param data
 */
rc.writeRenderedFile =
  (folder, file, data) => {
    const fullPath = rc.buildRenderedFilePath(folder, file);
    if (rc.exists(fullPath))
      fs.renameSync(fullPath,
        rc.buildRenderedFilePath(folder, file + '_' + rc.getFileTimeStamp(folder, file))
      );
    fs.writeFileSync(fullPath, data);
  };

/**
 * Reads the file $folder/$file and returns its contents,
 * if it doesn't exist the return value is Maybe.Nothing
 * @param folder
 * @param file
 * @returns Maybe(String)
 */
rc.readRenderedFile =
{'fraction_non_alphanumeric': 0.12785588752196836, 'fraction_numerical': 0.0004393673110720562, 'mean_word_length': 3.5213505461767625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3068481', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1382, 'n_words': 458}