Code coverage report for tasks/wrappers/sass.js

Statements: 87.88% (58 / 66)      Branches: 61.11% (11 / 18)      Functions: 94.74% (18 / 19)      Lines: 90.48% (57 / 63)      Ignored: none     

All files » tasks/wrappers/ » sass.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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991 1 1 1 1 1 1 1 1   1 1 1       1 5 5 5     1 2 2 2         2 2 1 1   1   2 2 2 2     2   2       2       2   2       2 2       1 1 1 1 1 1   1 1       1 1 1 1     1 1 1   1   1   1   1 1       1  
var autoprefixer = require('autoprefixer-core');
var postcss      = require('postcss');
var Promise = require('es6-promise').Promise;
var sass = require('node-sass');
var path = require('path');
var extend = require('util')._extend;
var fs = require('../utils/fs');
var File = require('../utils/file');
var log = require('../utils/log');
 
function wait(fileObjs){
    return new Promise(function(resolve, reject) {
        setTimeout(function(){ resolve(fileObjs); }, 100);
    });
}
 
function Sass(location, destination, options){
    this.location = location;
    this.destination = destination;
    this.options = options;
}
 
Sass.prototype.file = function(fileObj, outputStyle){
    var dir;
    var name = fileObj.name.replace('.scss','.css');
    var defaults = {
        file : fileObj.path,
        outputStyle : outputStyle || "nested",
        precision  : 3
    };
    var options = extend(defaults, this.options);
    if (outputStyle === 'compressed'){
        name = name.replace('.css','.min.css');
        dir = fileObj.dir;
    } else {
        dir = path.join(this.destination,fileObj.relativeDir);
    }
    var outFile = path.resolve(dir, name);
    var newFileObj = new File({path:outFile});
    return new Promise(function(resolve, reject){
        Iif (options.verbose){
            log.info('   * ' + fileObj.path.replace(options.appRoot,''));
        }
        sass.render(options,
            function(error, result){
                Iif (error){
                    log.warn('Sass Error');
                    reject(error);
                } else {
                    resolve(result);
                }
            });
    }).then(function (result) {
        return postcss([ autoprefixer ]).process(result.css);
    }).then(function(result){
        result.warnings().forEach(function (warn, i) {
            if (i===0) log.warn('Sass (autoprefixer) Error');
            log.warn(warn.toString());
        });
        newFileObj.contents = postcss([ autoprefixer ]).process(result.css).css;
        return newFileObj;
    });
};
 
Sass.prototype.minify = function(files){
    log.info(' * Minifying Styles: ' + this.destination);
    var self = this;
    var promises = [];
    files.forEach(function(fileObj){
        promises.push(self.file(fileObj, 'compressed'));
    });
    return Promise.all(promises).then(function(fileObjs){
        return fs.write(fileObjs);
    });
};
 
Sass.prototype.write = function() {
    var self = this;
    return fs.glob(this.location).then(function(files) {
        Iif (self.options.verbose && files.length===0){
            log.info('no scss files found: ' + self.location);
        }
        var promises = [];
        files.forEach(function (fileObj, i) {
            promises.push(self.file(fileObj));
        });
        return Promise.all(promises);
    }).then(function(fileObjs){
        return fs.write(fileObjs);
    }).then(function(fileObjs){
        return wait(fileObjs);
    }).then(function(fileObjs){
        Iif (!self.options.minify || self.options.dev) return Promise.resolve();
        return self.minify(fileObjs);
    });
};
 
module.exports = Sass;