<commit_before>'use strict'

const exists = require('fs').access;
const exec = require('child_process').exec;
const LRU = require('lru-cache');
const libob = require('libobject');
const cache_options = {max: 500};
const RE_method_path = /^<([^\/]+)\/([^#]+)(?:#([^\?]+))?\?(.*)>$/;

function saveRequire (module, method, callback) {
    try {
        let msg_method = method;
        if (!(method = libob.path.get(method, require(module)))) {
            return callback(new Error('Flow-nodejs.adapter.require: Method "' + msg_method + '" not found on module "' + module + '".'));
        }
        callback(null, method);
    } catch (err) {
        callback(err);
    }
}

module.exports = (store, entrypoint) => {

    return {
        cache: LRU(cache_options),
        seq: (scope, sequence_id, role) => {
            return store.sequence(sequence_id, role);
        },

        fn: (method_iri, role, cb) => {

            method_iri = method_iri.match(RE_method_path);
            if (!method_iri || !method_iri[1] || !method_iri[2] || !method_iri[4]) {
                return cb(new Error('Flow-nodejs.adapter.fn: Invalid method path.'));
            }

            // TODO require method with file path
            // module_root, module#version /paht/to/file.js

            let path = entrypoint.module_root + method_iri[2];
            exists(path, (err) => {
                if (err) {
                    return exec('npm i --prefix ' + entrypoint.base + ' ' + method_iri[1] + '/'  +  method_iri[2] + (method_iri[3] ? '#' + method_iri[3]: ''), err => {

                        if (err) {
                            return cb(err);
                        }

                        saveRequire(path, method_iri[4], cb);
                    });
                }

                saveRequire(path, method_iri[4], cb);
            });
        }
    };
};<commit_msg>require method from any module file<commit_after>'use strict'

const exists = require('fs').access;
const exec = require('child_process').exec;
const LRU = require('lru-cache');
const libob = require('libobject');
const cache_options = {max: 500};
const RE_method_path = /^<([^\/]+)\/([^#]+)(?:#([^\?]+))?\?(.*)>$/;

function saveRequire (module, method, callback) {
    try {
        let msg_method = method;
        if (!(method = libob.path.get(method, require(module)))) {
            return callback(new Error('Flow-nodejs.adapter.require: Method "' + msg_method + '" not found on module "' + module + '".'));
        }
        callback(null, method);
    } catch (err) {
        callback(err);
    }
}

module.exports = (store, entrypoint) => {

    return {
        cache: LRU(cache_options),
        seq: (scope, sequence_id, role) => {
            return store.sequence(sequence_id, role);
        },

        fn: (method_iri, role, cb) => {

            method_iri = method_iri.match(RE_method_path);
            if (!method_iri || !method_iri[1] || !method_iri[2] || !method_iri[4]) {
                return cb(new Error('Flow-nodejs.adapter.fn: Invalid method path.'));
            }

            let module = method_iri[2].indexOf('/') > -1 ? method_iri[2].split('/')[0] : method_iri[2];
            let file = entrypoint.module_root + method_iri[2];

            exists(entrypoint.module_root + module, (err) => {
                if (err) {
                    return exec('npm i --prefix ' + entrypoint.base + ' ' + method_iri[1] + '/'  +  module + (method_iri[3] ? '#' + method_iri[3]: ''), err => {

                        if (err) {
                            return cb(err);
                        }

                        saveRequire(file, method_iri[4], cb);
                    });
                }

                saveRequire(file, method_iri[4], cb);
            });
        }
    };
};
{'fraction_non_alphanumeric': 0.16903633491311215, 'fraction_numerical': 0.007898894154818325, 'mean_word_length': 1.8933739527798934, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, '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': '5061362', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1122, 'n_words': 233}