all files / lib/ index.js

97.14% Statements 34/35
83.33% Branches 15/18
87.5% Functions 7/8
90.91% Lines 10/11
1 statement, 3 branches Ignored     
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                                                                     
'use strict';
 
import CYSpriter from './helpers/CYSpriter.js';
import * as Q from 'q';
import * as _ from 'lodash';
import * as postcss from 'postcss';
import * as path from 'path';
import configs from './configs/default';
import { logger } from './helpers/utils';
 
function postcssCYSpriterInitializer(opts) {
  return function(css, result) {
 
    opts = _.merge({}, configs.default, opts, {
      src: path.resolve(opts.src || configs.default.src),
      dest: path.resolve(opts.dest || configs.default.dest),
      relativeTo: path.resolve(opts.relativeTo || configs.default.relativeTo)
    });
 
    return CYSpriter
      .study(opts, css, result)
      .spread((sprites, options) => {
        var promList = sprites.map(sprite => {
          return CYSpriter.run(sprite, options)
            .spread(CYSpriter.mapSprite)
            .spread(CYSpriter.save)
            .spread((sprite, options, sprites) => {
              return CYSpriter.writeRules(sprite, options, css)
            })
            ;
        });
 
        return Q.all(promList);
      })
      .catch(err => {
        logger.fatal(err);
      })
      ;
  };
}
 
module.exports =
  postcss.plugin('postcss-cyspriter', postcssCYSpriterInitializer);