Plato on Github
Report Home
lib/file_processor.js
Maintainability
69.80
Lines of code
193
Difficulty
29.80
Estimated Errors
1.25
Function weight
By Complexity
By SLOC
// Generated by CoffeeScript 1.6.2 /* This class compile and load files */ (function() { var CoffeeScript, Eco, FileProcessor, Jade, LRU, path, rejectOnInvalidFilenameType, _; path = require('path'); _ = require('lodash'); CoffeeScript = require('coffee-script'); Eco = require('eco'); Jade = require('jade'); LRU = require('lru-cache'); /* Checker method decorator */ rejectOnInvalidFilenameType = function(methodBody) { return function(filename, cb) { if (!_.isString(filename)) { return cb(TypeError("must be called with filename as String, but got:\n|filename| = |" + filename + "|")); } return methodBody.call(this, filename, cb); }; }; FileProcessor = (function() { var CS_BARE, MAX_AGE; CS_BARE = true; MAX_AGE = 1000 * 60 * 60 * 10; function FileProcessor(_file_loader_, _options_) { this._file_loader_ = _file_loader_; this._options_ = _options_ != null ? _options_ : {}; this._compiled_cache_ = LRU({ max: 1000, maxAge: MAX_AGE }); this._compilers_ = this._getAsyncCompilers(this._getJadeSettings(this._options_.jade)); _.assign(this._compilers_, this._options_.third_party_compilers); } /* This method load one file and, if it needed, compile it */ FileProcessor.prototype.loadFile = rejectOnInvalidFilenameType(function(filename, cb) { var file_ext, _this = this; file_ext = path.extname(filename); if (this._compilers_[file_ext] != null) { return this._file_loader_.getFileWithMeta(filename, function(err, data) { var content, digest, res; if (err) { return cb(err); } digest = data.digest; if (!_this._compiled_cache_.has(digest)) { content = data.content; return _this._compilers_[file_ext](_this._stripBOM(content), filename, function(err, result, must_be_parsed) { var res; if (err) { return cb(err); } res = ("\n// " + filename + " \n") + result; _this._compiled_cache_.set(digest, { must_be_parsed: must_be_parsed, compiled_data: res }); return cb(null, res, must_be_parsed, { digest: digest }); }); } else { res = _this._compiled_cache_.get(digest); return cb(null, res.compiled_data, res.must_be_parsed, { digest: digest }); } }); } else { return cb(null, false); } }); /* This method return supported extentions */ FileProcessor.prototype.getSupportedFileExtentions = function() { return _.keys(this._compilers_); }; /* This method reset all caches */ FileProcessor.prototype.resetCaches = function() { this._compiled_cache_.reset(); return null; }; /* This method from node.js lib/module // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) // because the buffer-to-string conversion in `fs.readFileSync()` // translates it to FEFF, the UTF-16 BOM. */ FileProcessor.prototype._stripBOM = function(content) { if (content.charCodeAt(0) === 0xFEFF) { return content.slice(1); } else { return content; } }; /* This internal method for Jade settings */ FileProcessor.prototype._getJadeSettings = function(options) { if (options == null) { options = {}; } return _.defaults({ client: true }, options, { pretty: true, self: true, compileDebug: false }); }; /* This is Async compilers list. @return - error, data, isRealCode (ie. may have 'require' and should to be processed) */ FileProcessor.prototype._getAsyncCompilers = function(jade_settings) { return { '.js': function(data, filename, cb) { return cb(null, data, true); }, '.json': function(data, filename, cb) { return cb(null, "module.exports = " + data); }, '.coffee': function(data, filename, cb) { var content; content = CoffeeScript.compile(data, { bare: CS_BARE }); return cb(null, content, true); }, '.eco': function(data, filename, cb) { var content; content = Eco.precompile(data); return cb(null, "module.exports = " + content); }, '.jade': function(data, filename, cb) { var content; content = Jade.compile(data, _.assign(jade_settings, { filename: filename })); return cb(null, "module.exports = " + content); } }; }; return FileProcessor; })(); module.exports = FileProcessor; }).call(this);