Code coverage report for tasks/wrappers/mustache.js

Statements: 30.19% (16 / 53)      Branches: 0% (0 / 6)      Functions: 6.67% (1 / 15)      Lines: 30.19% (16 / 53)      Ignored: none     

All files » tasks/wrappers/ » mustache.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 791 1 1 1 1 1   1 2 2 2     1                           1                       1                                     1                 1                   1  
var Promise = require('es6-promise').Promise;
var path = require('path');
var mustache = require('mustache');
var fs = require('../utils/fs');
var File = require('../utils/file');
var log = require('../utils/log');
 
function Mustache(location, destination, options){
    this.location = location;
    this.destination = destination;
    this.options = options;
}
 
Mustache.prototype.getPartialFile = function(base, partial){
    return new Promise(function(resolve, reject){
        var ext = partial.split('.');
        var fileName = (ext.length>1) ? partial : partial + '.*';
        fs.read(path.join(base , fileName)).then(function(fileObjs){
            var contents = [];
            fileObjs.forEach(function(fileObj){
                contents.push(fileObj.contents.toString('utf-8'));
            });
            resolve({partial: partial, contents: contents.join('\n')});
        }).catch(reject);
    });
};
 
Mustache.prototype.getPartials = function(fileObj){
    var self = this;
    var promises = [];
    var arrParsed = mustache.parse(fileObj.contents.toString('utf-8'));
    arrParsed.forEach(function (item) {
        if (item[0] == '>') {
            promises.push(self.getPartialFile(fileObj.base, item[1]));
        }
    });
    return Promise.all(promises);
};
 
Mustache.prototype.renderFile = function(fileObj){
    var self = this;
    var replacements = this.options;
    return this.getPartials(fileObj).then(function(partialObjs){
        var partials = {};
        partialObjs.forEach(function(partialObj){
            partials[partialObj.partial] = partialObj.contents;
        });
        var contents = mustache.render(fileObj.contents.toString(), replacements, partials);
        var outFile = path.join(self.destination, fileObj.relativeDir, fileObj.name);
        var file = new File({path: outFile, contents:contents});
        file.ext = 'html';
        if (self.options.verbose){
            log.info("   * " + fileObj.relativeDir + "/" + fileObj.name + " > " + file.path);
        }
        return file;
    });
};
 
Mustache.prototype.render = function(fileObjs){
    var self = this;
    var promises = [];
    fileObjs.forEach(function(fileObj){
        promises.push(self.renderFile(fileObj));
    });
    return Promise.all(promises);
};
 
Mustache.prototype.write = function(){
    var self = this;
    return fs.read(this.location)
        .then(function(fileObjs){
            return self.render(fileObjs);
        }).then(function(fileObjs){
            return fs.write(fileObjs);
        });
};
 
module.exports = Mustache;