<commit_before>/**
    * Provides a means to install missing dependencies at runtime.
    *
    * Written By:
    *              <NAME>
    *
    * License:
    *              MIT License. All code unless otherwise specified is
    *              Copyright (c) <NAME> and Contributors 2015.
    */

let exec = require('child_process').execSync,
    path = require('path'),
    fs = require('fs'),
    csHasLoaded = false,
    npmDirectory = global.rootPathJoin('node_modules'),
    nativeModules = Object.getOwnPropertyNames(process.binding('natives')),

    command = function(args) {
        args.unshift('--silent');
        args.unshift('npm');
        let cmd = args.join(' ');
        exec(cmd, {cwd:global.__rootPath});
    },

    // inject require modifications into all coffeescript code because babel wont
    coffeescriptRequireInjector = function () {
        if (!csHasLoaded && global.coffeescriptLoaded) {
            let cs = require('coffee-script'),
                orig = cs._compileFile,
                requirejs = require('./require.js');
            cs._compileFile = function () {
                let res = orig.apply(this, arguments);
                return requirejs.injectionString + res;
            };
            csHasLoaded = true;
        }
    },

    install = function(name) {
        let t = global.$$, // translations might not have loaded
            startStr = t ? t`Installing "${name}" from npm.` : `Installing "${name}" from npm.`,
            endStr = t ? t`Installation complete.` : `Installation complete.`;

        console.info(startStr);
        command(['install', name]);
        console.info(endStr);
    },

    resolve = function(request, dirName) {
        try {
            if (nativeModules.includes(request)) {
                return true;
            }
            let parsed = path.parse(request);
            if (parsed.ext !== '' && parsed.ext !== '.' && parsed.dir !== '') {
                let p = path.resolve(dirName, request),
                    file = fs.statSync(p);
                return file && (file.isFile() || file.isDirectory());
            }
            else {
                let dir = fs.statSync(path.join(npmDirectory, request));
                return dir && dir.isDirectory();
            }
        }
        catch (e) {
            return false;
        }
    };

exports.requireOrInstall = function (req, name, dirName) {
    coffeescriptRequireInjector();
    if (resolve(name, dirName)) {
        return req(name);
    }

    install(name);
    return req(name);
};

exports.update = function() {
    command(['update']);
};
<commit_msg>Fix/require modules on restart (#223)<commit_after>/**
    * Provides a means to install missing dependencies at runtime.
    *
    * Written By:
    *              <NAME>
    *
    * License:
    *              MIT License. All code unless otherwise specified is
    *              Copyright (c) <NAME> and Contributors 2015.
    */

let exec = require('child_process').execSync,
    path = require('path'),
    fs = require('fs'),
    csHasLoaded = false,
    nativeReloadHacksCache = {},
    nativeReloadHacks = ['deasync'],
    npmDirectory = global.rootPathJoin('node_modules'),
    nativeModules = Object.getOwnPropertyNames(process.binding('natives')),

    command = function(args) {
        args.unshift('--silent');
        args.unshift('npm');
        let cmd = args.join(' ');
        exec(cmd, {cwd:global.__rootPath});
    },

    // inject require modifications into all coffeescript code because babel wont
    coffeescriptRequireInjector = function () {
        if (!csHasLoaded && global.coffeescriptLoaded) {
            let cs = require('coffee-script'),
                orig = cs._compileFile,
                requirejs = require('./require.js');
            cs._compileFile = function () {
                let res = orig.apply(this, arguments);
                return requirejs.injectionString + res;
            };
            csHasLoaded = true;
        }
    },

    install = function(name) {
        let t = global.$$, // translations might not have loaded
            startStr = t ? t`Installing "${name}" from npm.` : `Installing "${name}" from npm.`,
            endStr = t ? t`Installation complete.` : `Installation complete.`;

        console.info(startStr);
        command(['install', name]);
        console.info(endStr);
    },

    resolve = function(request, dirName) {
        try {
            if (nativeModules.includes(request)) {
                return true;
            }
            let parsed = path.parse(request);
            if (parsed.ext !== '' && parsed.ext !== '.' && parsed.dir !== '') {
                let p = path.resolve(dirName, request),
                    file = fs.statSync(p);
                return file && (file.isFile() || file.isDirectory());
            }
            else {
                let dir = fs.statSync(path.join(npmDirectory, request));
                return dir && dir.isDirectory();
            }
        }
        catch (e) {
            return false;
        }
    };

exports.requireOrInstall = function (req, name, dirName) {
    coffeescriptRequireInjector();
    if (!resolve(name, dirName)) {
        install(name);
    }
    else if (nativeReloadHacksCache.hasOwnProperty(name)) {
        return nativeReloadHacksCache[name];
    }
    let r = req(name);

    if (nativeReloadHacks.includes(name) || name.endsWith('.node')) {
        nativeReloadHacksCache[name] = r;
    }
    return r;
};

exports.update = function() {
    command(['update']);
};

{'fraction_non_alphanumeric': 0.1330579996408691, 'fraction_numerical': 0.0019752199676782187, 'mean_word_length': 1.9162303664921465, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4390555', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1509, 'n_words': 352}