Skip to content

Commit

Permalink
Add support to parse and save multiple files. fixes ehynds#19
Browse files Browse the repository at this point in the history
If `dest` param is a directory — each input file will be saved into separate file in `dest` directory (with same name). Add `cwd` param to add base path for input files.
  • Loading branch information
afitiskin committed Aug 10, 2013
1 parent 3e5ee63 commit e00c044
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions tasks/grunt-image-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
* Licensed under the MIT license.
*/

// External libs
var path = require('path');

// Internal libs
var grunt_encode = require("./lib/encode");

module.exports = function(grunt) {
module.exports = function (grunt) {
"use strict";

// Grunt lib init
Expand All @@ -18,25 +21,56 @@ module.exports = function(grunt) {
// Grunt utils
var async = grunt.util.async;

grunt.registerMultiTask("imageEmbed", "Embed images as base64 data URIs inside your stylesheets", function() {
grunt.registerMultiTask("imageEmbed", "Embed images as base64 data URIs inside your stylesheets", function () {
var opts = this.options();
var done = this.async();

// Process each src file
this.files.forEach(function(file) {
this.files.forEach(function (file) {
var cwd = file.cwd;
var dest = file.dest;
var tasks;
var destIsFile = !!path.extname(dest);

var src = file.src.filter(function(src) {
if (cwd) {
src = path.join(cwd, src);
}

if (grunt.file.isFile(src)) {
return true;
} else {
grunt.log.warn('Source file "' + src + '" not found.');
return false;
}
});

tasks = file.src.map(function(srcFile) {
return function(callback) {
if (src.length > 1 && destIsFile) {
grunt.log.warn('Source file cannot be more than one when dest is a file.');
}


var tasks = file.src.map(function (srcFile) {
return function (callback) {
if (cwd) {
srcFile = path.join(cwd, srcFile);
}
encode.stylesheet(srcFile, opts, callback);
};
});

// Once all files have been processed write them out.
async.parallel(tasks, function(err, output) {
grunt.file.write(dest, output);
grunt.log.writeln('File "' + dest + '" created.');
async.parallel(tasks, function (err, output) {
if (destIsFile) {
grunt.file.write(dest, content);
grunt.log.writeln('File "' + dest + '" created.');
} else {
output.forEach(function (content, index) {
var outputPath = path.join(dest, src[index]);
grunt.file.write(outputPath, content);
grunt.log.writeln('File "' + outputPath + '" created.');
});
}

done();
});
});
Expand Down

1 comment on commit e00c044

@ehynds
Copy link

@ehynds ehynds commented on e00c044 Dec 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

care to open a PR for this?

Please sign in to comment.