<commit_before>var path = require('path');
var fs = require('fs');
var resolve = require('app-root-path').resolve;
var readdir = fs.readdirSync;
var lstat = fs.lstatSync;
var exists = fs.existsSync;

function LoadRoutes(app, target) {
    if (!(this instanceof LoadRoutes)) {
        return new LoadRoutes(app, target);
    }
    this.init(app, target);
}

// export module
module.exports = LoadRoutes;

/**
 * Inject the routes and routers into the express instance
 * @param  {Express} app
 * @param  {String} target
 */
LoadRoutes.prototype.init = function(app, target) {
    target = resolve(typeof target === 'string' ? target : 'routes');
    this.readdir(target).forEach(function(file) {
        if (this.isHiddenFile(file))
            return;
        var route, router;
        route = this.pathToRoute(file, target);
        router = require(file);
        if (typeof router !== 'function')
            return;
        app.use(route, router);
    }, this);
};

/**
 * Reads all the files and folder within a given target
 * @param  {String} target
 * @return {Array}
 */
LoadRoutes.prototype.readdir = function(target) {
    var files = [];

    if (typeof target !== 'string') {
        throw new TypeError('Expecting target path to be a string');
    }

    if (target.charAt(0) === '.') {
        // resolve the target path
        target = path.resolve(path.dirname(module.parent.filename), target);
    }

    // return an empty array if target does not exists
    if (!exists(target)) return files;

    // look for files recursively
    readdir(target).forEach(function(file) {
        var filePath = path.join(target, file);

        if (isFile(filePath)) {
            files.push(filePath);
        } else {
            files.push.apply(files, this.readdir(filePath));
        }

    }, this);

    return files;
};

/**
 * Check if target file is a hidden file
 * @param  {String} target
 * @return {Boolean}
 */
LoadRoutes.prototype.isHiddenFile = function(target) {
    // remove file extension and normalize slashes
    target = path.normalize(target);
    target = target.replace(path.extname(target), '');

    var splitTarget = target.split('/');
    var file = splitTarget[splitTarget.length - 1];

    if (file.charAt(0) === '.')
        return true;
    return false;
};

/**
 * Convert a file path into an express route
 * @param  {String} path
 * @param  {String} base
 * @return {String}
 */
LoadRoutes.prototype.pathToRoute = function(target, base) {
    // remove file extension and normalize slashes
    target = path.normalize(target);
    target = target.replace(path.extname(target), '');

    if (base && typeof base === 'string') {
        var segments = [],
            segment;

        target = target.split('/');
        base = path.normalize(base).split('/');
        base = base[base.length - 1];

        for (var i = target.length - 1; i >= 0; i--) {
            segment = target[i];
            if (segment === base)
                break;
            // check if segment is a hidden file or an index
            if (i === target.length - 1 && segment === 'index')
                continue;
            if (segment !== '')
                segments.push(segment);
        }
        return '/' + segments.reverse().join('/');
    }
    // without a base, use the last segment
    target = path.basename(target);
    return '/' + (target !== 'index' ? target : '');
};

function isFile(target) {
    return lstat(target).isFile();
}

function isDir(target) {
    return lstat(target).isDirectory();
}<commit_msg>updated lib<commit_after>import path from 'path';
import resolve from './resolve';

export default function lib(dirname = __dirname) {
  let appRootPath = resolve(dirname);
  let publicInterface = {
    resolve: (pathToModule) => {
      return path.join(appRootPath, pathToModule);
    },

    require: (pathToModule) => {
      // Backwards compatibility check
      if ('function' === typeof pathToModule) {
        console.warn('Just use appRootPath.require() -- no need to pass in your ' +
               'modules\'s require() function any more.');
        return (pathToModule) => {
          return publicInterface.require(pathToModule);
        };
      }

      return require(publicInterface.resolve(pathToModule));
    },

    toString: () => {
      return appRootPath;
    },

    setPath: (explicitlySetPath) => {
      appRootPath = path.resolve(explicitlySetPath);
      publicInterface.path = appRootPath;
    },

    path: appRootPath
  };

  return publicInterface;
}
{'fraction_non_alphanumeric': 0.13952464788732394, 'fraction_numerical': 0.0015404929577464788, 'mean_word_length': 2.525989138867339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2913821', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1303, 'n_words': 356}