Plato on Github
Report Home
lib/gatherer.js
Maintainability
64.47
Lines of code
276
Difficulty
59.26
Estimated Errors
2.33
Function weight
By Complexity
By SLOC
// Generated by CoffeeScript 1.6.2 /* This class build whole codebase for path Короче, здесь у нас модуль, который получает на вход путь и возвращает полный комплект из путей (для разрешения require) и скомпилированное содерживое файлов */ (function() { var Gatherer, LRU, Resolver, async, detective, fs, path, util, _, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __slice = [].slice; fs = require('fs'); path = require('path'); _ = require('lodash'); async = require('async'); detective = require('detective'); LRU = require('lru-cache'); util = require('util'); Resolver = require('async-resolve'); Gatherer = (function() { var MAX_AGE; MAX_AGE = 1000 * 60 * 60 * 10; function Gatherer(_file_processor_, _options_) { var _ref; this._file_processor_ = _file_processor_; this._options_ = _options_ != null ? _options_ : {}; this._dependenciesTreeSaver = __bind(this._dependenciesTreeSaver, this); this._findRequiresAndAddToQueue = __bind(this._findRequiresAndAddToQueue, this); this._queueFn = __bind(this._queueFn, this); this._pathfinder_ = new Resolver(); (_ref = this._pathfinder_).addExtensions.apply(_ref, this._file_processor_.getSupportedFileExtentions()); this._require_cache_ = LRU({ max: 1000, maxAge: MAX_AGE }); } /* This method reset all caches */ Gatherer.prototype.resetCaches = function() { this._require_cache_.reset(); return null; }; /* This is Async version of packer */ Gatherer.prototype.buildModulePack = function(path_name, options, main_cb) { var load_queue, pack_cache, _this = this; if (options == null) { options = {}; } pack_cache = { dependencies_tree: {}, names_map: {}, source_code: {}, err: null, filters: [], requireless: [] }; if (options.filters != null) { pack_cache.filters = this._forceFilterToArray(options.filters); } if (options.requireless != null) { pack_cache.requireless = this._forceFilterToArray(options.requireless); } pack_cache.queue_obj = load_queue = async.queue(this._queueFn, 50); load_queue.drain = function() { if (!pack_cache.err) { return main_cb(null, { dependencies_tree: pack_cache.dependencies_tree, names_map: pack_cache.names_map, source_code: pack_cache.source_code }); } else { return main_cb(pack_cache.err); } }; return load_queue.push({ path_name: path_name, parent: '.', pack_cache: pack_cache }, function(err) { return pack_cache.err = err; }); }; /* Oh! Many things here, but its price of async code */ Gatherer.prototype._queueFn = function(_arg, queue_cb) { var pack_cache, parent, path_name, _this = this; path_name = _arg.path_name, parent = _arg.parent, pack_cache = _arg.pack_cache; return async.waterfall([ function(waterfall_cb) { return _this._pathfinder_.resolveAbsolutePath(path_name, path.dirname(parent), waterfall_cb); }, function(real_file_name, waterfall_cb) { if (!_this._dependenciesTreeSaver({ path_name: path_name, parent: parent, real_file_name: real_file_name, pack_cache: pack_cache })) { return queue_cb(); } return _this._file_processor_.loadFile(real_file_name, function(err, content, may_have_reqire, _arg1) { var digest; digest = _arg1.digest; if (err) { return waterfall_cb(err); } return waterfall_cb(null, { digest: digest, content: content, may_have_reqire: may_have_reqire, path_name: path_name, real_file_name: real_file_name }); }); }, function(_arg1, waterfall_cb) { var content, digest, may_have_reqire, path_name, real_file_name; digest = _arg1.digest, content = _arg1.content, may_have_reqire = _arg1.may_have_reqire, path_name = _arg1.path_name, real_file_name = _arg1.real_file_name; _this._fileDataSaver({ digest: digest, content: content, real_file_name: real_file_name, pack_cache: pack_cache }); _this._findRequiresAndAddToQueue({ digest: digest, may_have_reqire: may_have_reqire, content: content, real_file_name: real_file_name, path_name: path_name, pack_cache: pack_cache }); return waterfall_cb(); } ], function(err) { return queue_cb(err); }); }; /* This is converter for ensure filter is Array @arg may be one value or Array */ Gatherer.prototype._forceFilterToArray = function() { var filters_list, first_filter, other_filters; first_filter = arguments[0], other_filters = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return filters_list = !_.isArray(first_filter) ? [first_filter].concat(other_filters) : first_filter; }; /* This method searching for requires, its just stub. later I re-write it to class to substitute detective with my own logic and acorn */ Gatherer.prototype._findRequiresItself = function(data) { return detective(data); }; /* This method find requires in files, if they need it and add to queue new files for recurse working */ Gatherer.prototype._findRequiresAndAddToQueue = function(_arg) { var child, childrens, content, digest, may_have_reqire, pack_cache, path_name, real_file_name, res, _i, _len; digest = _arg.digest, may_have_reqire = _arg.may_have_reqire, content = _arg.content, real_file_name = _arg.real_file_name, path_name = _arg.path_name, pack_cache = _arg.pack_cache; if (may_have_reqire === true && this._isFilesMustBeProcessed(pack_cache.requireless, path_name)) { childrens = !this._require_cache_.has(digest) ? (res = this._findRequiresItself(content), this._require_cache_.set(digest, res), res) : this._require_cache_.get(digest); for (_i = 0, _len = childrens.length; _i < _len; _i++) { child = childrens[_i]; pack_cache.queue_obj.push({ path_name: child, parent: real_file_name, pack_cache: pack_cache }, function(err) { return pack_cache.err = err; }); null; } } return null; }; /* This part-method handle save to tree if file exists or unnided - return false */ Gatherer.prototype._dependenciesTreeSaver = function(_arg) { var dep_tree_par, pack_cache, parent, path_name, real_file_name, _base, _ref; path_name = _arg.path_name, parent = _arg.parent, real_file_name = _arg.real_file_name, pack_cache = _arg.pack_cache; dep_tree_par = (_ref = (_base = pack_cache.dependencies_tree)[parent]) != null ? _ref : _base[parent] = {}; if (dep_tree_par[path_name] != null) { return false; } if (!this._isFilesMustBeProcessed(pack_cache.filters, path_name)) { dep_tree_par[path_name] = null; return false; } if (this._pathfinder_.isCoreModule(real_file_name)) { dep_tree_par[path_name] = null; return false; } dep_tree_par[path_name] = real_file_name; return true; }; /* This method save data void return */ Gatherer.prototype._fileDataSaver = function(_arg) { var content, digest, pack_cache, real_file_name, _base, _ref; digest = _arg.digest, content = _arg.content, real_file_name = _arg.real_file_name, pack_cache = _arg.pack_cache; pack_cache.names_map[real_file_name] = digest; if ((_ref = (_base = pack_cache.source_code)[real_file_name]) == null) { _base[real_file_name] = content; } return null; }; /* This is proceed filter return 'true' if file MUST be processed, ie it filename NOT in list - it will be processed */ Gatherer.prototype._isFilesMustBeProcessed = function(filters_list, path_name) { return !_.any(filters_list, function(filter) { return filter === path_name; }); }; return Gatherer; })(); module.exports = Gatherer; }).call(this);