Sha256: 4d612c10307a92bc5c205db767d4c183b3fc46ff927e9fe302864c084228f987

Contents?: true

Size: 1.75 KB

Versions: 12

Compression:

Stored size: 1.75 KB

Contents

"use strict";

module.exports = function(grunt){
  grunt.registerMultiTask('svg2png', 'Convert SVG to PNGs', function(){

    var svg_to_png = require('svg-to-png'),
        path       = require('path'),
        fs         = require('fs'),
        async      = require('async'),
        fileHelper = require('../modules/file'),
        Promise    = require('bluebird'),
        dest       = this.data.src,
        files      = grunt.file.expand(dest + "**/*.svg"),
        done       = this.async(),
        tmpPath    = "tmp/",
        sizes      = this.data.sizes,
        promises   = [];

    deleteFolderRecursive(tmpPath);

    files.forEach(function(file){
      var svg = grunt.file.read(file);
      var newSvg = svg.replace(/width=\"[0-9]+\" height=\"[0-9]+\"/, '');
      fileHelper.writeFile(newSvg, tmpPath + path.basename(file), "Icon");
    });

    sizes.forEach(function(size){
      var sizing = size.split(" ");
      var sizeX = sizing[0];
      var sizeY = sizing[1];
      var tmpFiles = grunt.file.expand(tmpPath + "**/*.svg");
      promises.push(convert(tmpFiles, sizeX, sizeY))
    });

    Promise.all(promises).then(function(){
      done();
    });

    function convert(files, sizeX, sizeY){
      return svg_to_png.convert(files, path.join( dest + sizeX + "px"), {defaultWidth: sizeX, defaultHeight: sizeY, compress: true})
    }

    function deleteFolderRecursive(path){
      if( fs.existsSync(path) ) {
        fs.readdirSync(path).forEach(function(file,index){
          var curPath = path + "/" + file;
          if(fs.lstatSync(curPath).isDirectory()) { // recurse
            deleteFolderRecursive(curPath);
          } else { // delete file
            fs.unlinkSync(curPath);
          }
        });
        fs.rmdirSync(path);
      }
    }
  });
}

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ustyle-1.11.1 grunt/tasks/svg2png.js
ustyle-1.10.0 grunt/tasks/svg2png.js
ustyle-1.8.7 grunt/tasks/svg2png.js
ustyle-1.8.1 grunt/tasks/svg2png.js
ustyle-1.8.0 grunt/tasks/svg2png.js
ustyle-1.7.0 grunt/tasks/svg2png.js
ustyle-1.6.1 grunt/tasks/svg2png.js
ustyle-1.5.8 grunt/tasks/svg2png.js
ustyle-1.4.1 grunt/tasks/svg2png.js
ustyle-1.4.0 grunt/tasks/svg2png.js
ustyle-1.3.12 grunt/tasks/svg2png.js
ustyle-1.3.6 grunt/tasks/svg2png.js