<commit_before>'use strict';

var arrayUniq = require('array-uniq');
var detective = require('detective');
var path = require('path');
var looksLikePath = require('./util/looksLikePath');
var noError = require('./util/noError');
var readFile = require('./readFile');
var sequence = require('./util/sequence');

function resolveBundleDeps(bundle, resolver, cb) {
    if (bundle._resolvingDeps) {
        bundle.once('resolve', cb);
        return;
    }

    bundle._resolvingDeps = true;
    var finish = function (err) {
        cb(err);
        bundle._resolvingDeps = false;
        bundle.emit('resolve', err);
    };

    // our externals' requires must be resolved first
    var forEachBundle = function (externalBundle, cb) {
        resolveBundleDeps(externalBundle, resolver, cb);
    };

    sequence(bundle._externals, forEachBundle, noError(finish, function () {
        function forEachFile(filename, cb) {
            resolveFileDeps(bundle, bundle._files[filename], resolver, cb);
        }
        sequence(Object.keys(bundle._files), forEachFile, finish);
    }));
}

function resolveFileDeps(bundle, file, resolver, cb) {
    readFile(bundle, file, noError(cb, function (buf) {
        var requires = arrayUniq(detective(buf));

        function forEachId(id, cb) {
            resolveRequire(bundle, file, id, resolver, cb);
        }

        sequence(requires, forEachId, cb);
    }));
}

function resolveRequire(bundle, sourceFile, id, resolver, cb) {
    function addDep(depFile) {
        sourceFile._deps[id] = depFile._hash;
        cb(null);
    }

    // check if id is exposed by the current bundle or an external bundle
    var file = getExposedFile(bundle, id) || findExternalFile(bundle._externals, function (externalBundle) {
        return getExposedFile(externalBundle, id);
    });

    if (file) {
        addDep(file);
        return;
    }

    resolver.resolve(id, sourceFile.path, function (err, depFilename) {
        if (err) {
            if (looksLikePath(id)) {
                depFilename = path.resolve(path.dirname(sourceFile.path), id);
            } else {
                cb(err);
                return;
            }
        }

        // check if the file exists in an external bundle
        var file = bundle._files[depFilename] || findExternalFile(bundle._externals, function (externalBundle) {
            return externalBundle._files[depFilename];
        });

        if (file) {
            addDep(file);
            return;
        }

        // id is not a path; make sure we expose the actual resolved path,
        // so dependent bundles can require the same identifer from us.
        if (!looksLikePath(id) && !(id in bundle._exposed)) {
            bundle._exposed[id] = depFilename;
        }

        // wasn't found in our own bundle or any external one, add it to ours
        bundle.require(depFilename);

        file = bundle._files[depFilename];

        resolveFileDeps(bundle, file, resolver, noError(cb, addDep.bind(null, file)));
    });
}

function findExternalFile(externals, callback) {<commit_msg>fix comment<commit_after>'use strict';

var arrayUniq = require('array-uniq');
var detective = require('detective');
var path = require('path');
var looksLikePath = require('./util/looksLikePath');
var noError = require('./util/noError');
var readFile = require('./readFile');
var sequence = require('./util/sequence');

function resolveBundleDeps(bundle, resolver, cb) {
    if (bundle._resolvingDeps) {
        bundle.once('resolve', cb);
        return;
    }

    bundle._resolvingDeps = true;
    var finish = function (err) {
        cb(err);
        bundle._resolvingDeps = false;
        bundle.emit('resolve', err);
    };

    // our externals' requires must be resolved first
    var forEachBundle = function (externalBundle, cb) {
        resolveBundleDeps(externalBundle, resolver, cb);
    };

    sequence(bundle._externals, forEachBundle, noError(finish, function () {
        function forEachFile(filename, cb) {
            resolveFileDeps(bundle, bundle._files[filename], resolver, cb);
        }
        sequence(Object.keys(bundle._files), forEachFile, finish);
    }));
}

function resolveFileDeps(bundle, file, resolver, cb) {
    readFile(bundle, file, noError(cb, function (buf) {
        var requires = arrayUniq(detective(buf));

        function forEachId(id, cb) {
            resolveRequire(bundle, file, id, resolver, cb);
        }

        sequence(requires, forEachId, cb);
    }));
}

function resolveRequire(bundle, sourceFile, id, resolver, cb) {
    function addDep(depFile) {
        sourceFile._deps[id] = depFile._hash;
        cb(null);
    }

    // check if id is exposed by the current bundle or an external bundle
    var file = getExposedFile(bundle, id) || findExternalFile(bundle._externals, function (externalBundle) {
        return getExposedFile(externalBundle, id);
    });

    if (file) {
        addDep(file);
        return;
    }

    resolver.resolve(id, sourceFile.path, function (err, depFilename) {
        if (err) {
            if (looksLikePath(id)) {
                depFilename = path.resolve(path.dirname(sourceFile.path), id);
            } else {
                cb(err);
                return;
            }
        }

        // check if the file already exists in this bundle or an external one
        var file = bundle._files[depFilename] || findExternalFile(bundle._externals, function (externalBundle) {
            return externalBundle._files[depFilename];
        });

        if (file) {
            addDep(file);
            return;
        }

        // id is not a path; make sure we expose the actual resolved path,
        // so dependent bundles can require the same identifer from us.
        if (!looksLikePath(id) && !(id in bundle._exposed)) {
            bundle._exposed[id] = depFilename;
        }

        // wasn't found in our own bundle or any external one, add it to ours
        bundle.require(depFilename);

        file = bundle._files[depFilename];

        resolveFileDeps(bundle, file, resolver, noError(cb, addDep.bind(null, file)));
    });
}

function findExternalFile(externals, callback) {
{'fraction_non_alphanumeric': 0.11836998706338939, 'fraction_numerical': 0.0, 'mean_word_length': 2.5545977011494254, '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': '703927', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1721, 'n_words': 457}