Skip to content

Commit

Permalink
Allow to specify array of files as src, that resolve gcpantazis#10
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Derks committed Mar 10, 2015
1 parent 7652db9 commit 97917c1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ module.exports = function(grunt) {
options: {
resize: '300x300'
}
},

SrcAsArray: {
imageDirectory: IMAGES,
src: [
IMAGES + '/deep/directory/foo/*.png'
],
destination: 'tmp/optimized/src_as_array',
outputSuffix: '_medium',
keepDirectoryStructure: false,
options: {
resize: '66%'
}
}
},

Expand Down
36 changes: 31 additions & 5 deletions tasks/grunt-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module.exports = function(grunt) {
dest = currentTask.data.destination,
keepDirectoryStructure = currentTask.data.keepDirectoryStructure,
outputSuffix = currentTask.data.outputSuffix,
files = grunt.file.expand(currentTask.data.files),
fileCount = files.length;
files = getFiles(currentTask),
fileCount = getFileCount(files);

if ( !dest && outputSuffix ) {
grunt.log.error('Images Error: "outputSuffix" was set, but "destination" was not. Please set both if using "outputSuffix".');
Expand All @@ -34,7 +34,7 @@ module.exports = function(grunt) {
return;
}

if ( files.length === 0 ) {
if ( fileCount === 0 ) {
grunt.log.error('Images Error: No images matched.');
done(true);
return;
Expand All @@ -46,8 +46,7 @@ module.exports = function(grunt) {
grunt.file.mkdir(dest);
}

files.forEach(function( filepath ) {

eachFile(files, function( filepath ) {
var tempFilePath;

if ( !filepath.match(imageDirectory) ) {
Expand Down Expand Up @@ -191,4 +190,31 @@ module.exports = function(grunt) {
callback(err);
});
};

var eachFile = function (files, cb) {
files.forEach(function (file) {
file.src.forEach(cb);
});
};

var getFileCount = function (files) {
var count = 0;

files.forEach(function (file) {
count += file.src.length;
});

return count;
};

var getFiles = function (currentTask) {
//Backward compatibility
if (typeof currentTask.data.files === "string") {
return [{
src: grunt.file.expand(currentTask.data.files)
}];
}

return currentTask.files;
};
};
10 changes: 8 additions & 2 deletions tests/convert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.images = {

var actual, expected;

test.expect(3);
test.expect(4);

actual = grunt.file.read('tmp/optimized/jpeg_resizes/nasa_thumb.jpg').length;
expected = grunt.file.read('tests/expected/jpeg_resizes/nasa_thumb.jpg').length;
Expand All @@ -26,6 +26,12 @@ exports.images = {
return 'PNG 33% Resize - Actual size ('+actual+') within 3% of expected size ('+expected+').';
})());

actual = grunt.file.exists('tmp/optimized/src_as_array/mag-glass_medium.png');
expected = true;
test.ok(actual === expected, (function(){
return 'Expected file "tmp/optimized/src_as_array/mag-glass_medium.png" to exists';
})());

test.done();
}
};
};
Binary file modified tests/expected/mag-glass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/pngquant_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ exports.images = {

test.done();
}
};
};

0 comments on commit 97917c1

Please sign in to comment.