<commit_before>var browserify = require('browserify'),
    fs = require('fs'),
    resumer = require('resumer'),
    path = require('path'),
    childProcess = require("child_process"),
    exec = childProcess.exec,
    stream = require('stream'),
    defaults = {
        installDirectory: './globalify_modules',
        version: 'x.x.x'
    };

module.exports = function globalify(settings, callback){

    settings = settings || {};

    for(var key in defaults){
        settings[key] = settings[key] || defaults[key];
    }

    var moduleName = settings.module,
        version = settings.version,
        outputFileName = moduleName + '-' + version.replace(/\./g,'-') + '.js',
        outputStream = stream.PassThrough(),
        bundleStream;

    function globalifyModule(moduleName, callback){

        var stream = resumer().queue('window["' + (settings.globalVariable || moduleName) + '"] = require("' + moduleName + '");').end();
        var b = browserify({
            entries: [stream],
            basedir: path.resolve(process.cwd(), settings.installDirectory)
        });

        bundleStream = b.bundle(callback).pipe(outputStream);
    }

    function installModule(moduleName, version, callback){
        var installDirectory = path.resolve(process.cwd(), settings.installDirectory),
            packagePath = path.join(installDirectory, 'package.json');

        if(!fs.existsSync(installDirectory)){
            fs.mkdirSync(installDirectory);
        }

        if(!fs.existsSync(packagePath)){
            fs.writeFileSync(packagePath, JSON.stringify({
                name:'globalify-modules'
            }));
        };

        exec('npm install ' + moduleName + '@"' + version + '"',
        {
            cwd: installDirectory
        },
        function(error){
            callback(error);
        });
    }

    installModule(moduleName, version, function(error){
        if(error){
            callback(error);
            return;
        }<commit_msg>switched to API version of NPM<commit_after>var browserify = require('browserify'),
    fs = require('fs'),
    npm = require('npm'),
    resumer = require('resumer'),
    path = require('path'),
    stream = require('stream'),
    defaults = {
        installDirectory: './globalify_modules',
        version: 'x.x.x'
    };

module.exports = function globalify(settings, callback){

    settings = settings || {};

    for(var key in defaults){
        settings[key] = settings[key] || defaults[key];
    }

    var moduleName = settings.module,
        version = settings.version,
        outputFileName = moduleName + '-' + version.replace(/\./g,'-') + '.js',
        outputStream = stream.PassThrough(),
        bundleStream;

    function globalifyModule(moduleName, callback){

        var stream = resumer().queue('window["' + (settings.globalVariable || moduleName) + '"] = require("' + moduleName + '");').end();
        var b = browserify({
            entries: [stream],
            basedir: path.resolve(process.cwd(), settings.installDirectory)
        });

        bundleStream = b.bundle(callback).pipe(outputStream);
    }

    function npmInstall(module, version, callback){
        var nameAndVersion = module + (version ? '@"' + version + '"' : '');
        npm.commands.install([nameAndVersion], function (error, data) {
            callback(error);
        });
    }

    function installModule(moduleName, version, callback){
        var installDirectory = path.resolve(process.cwd(), settings.installDirectory),
            packagePath = path.join(installDirectory, 'package.json');

        if(!fs.existsSync(installDirectory)){
            fs.mkdirSync(installDirectory);
        }

        if(!fs.existsSync(packagePath)){
            fs.writeFileSync(packagePath, JSON.stringify({
                name:'globalify-modules'
            }));
        }

        npm.load({
                prefix: installDirectory
            },
            function(error) {
                if(!version || version === 'x.x.x'){
                    npmInstall(moduleName, null, callback);
                }else{
                    npmInstall(moduleName, version, callback);
                }
            }
        );
    }

    installModule(moduleName, version, function(error){
        if(error){
            callback(error);
            return;
        }
{'fraction_non_alphanumeric': 0.1419710544452102, 'fraction_numerical': 0.0, 'mean_word_length': 2.2468307233407905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1008582', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1121, 'n_words': 217}