0

I have the following array in Gulp:

var imageFiles = {
    'bootstrap-fileinput': 'bower/bootstrap-fileinput/dist/img/*', 
    'some-other-thing':    'bower/some-other-thing/dist/img/*'
};

I can be restructured if needed, as long as there is a prefix and path for that prefix.

How do I make gulp copy my images to the single folder I want, but prefix each one with prefix from the array? Means:

bower/bootstrap-fileinput/dist/img/arrow.jpg => /images/bootstrap-fileinput_arrow.jpg

bower/some-other-thing/dist/img/arrow.jpg => /images/some-other-thing_arrow.jpg

27.08.14: Do I understand correctly, that the task I described cannot be achieved using only one pipe? In the solution provided by @Doodlebot each file is piped independently and sequentially as I can see.

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156

1 Answers1

2

I was able to do this using the gulp-rename plug-in:

var gulp = require('gulp');
var rename = require('gulp-rename');

gulp.task('default', function() {
    var imageFiles = {
        'bootstrap-fileinput': 'bower/bootstrap-fileinput/dist/img/*',
        'some-other-thing': 'bower/some-other-thing/dist/img/*'
    }

    Object.keys(imageFiles).forEach(function(key) {
        gulp.src(imageFiles[key])
        .pipe(rename({
            prefix: key
        }))
        .pipe(gulp.dest('images/'));
    });
});
Doodlebot
  • 926
  • 5
  • 9
  • 2
    Just an FYI: in Node (and modern browsers), you can use `Object.keys(imageFiles).forEach(function(key) { … })` to iterate over the keys without worrying about `hasOwnProperty`. [More info here.](http://stackoverflow.com/questions/7440001/iterate-over-object-keys-in-node-js) – OverZealous Aug 26 '14 at 19:09
  • Do I understand correctly, that the task I described cannot be achieved using only one pipe? In the solution you provided each file is piped independently and sequentially as I can see. – Vladislav Rastrusny Aug 27 '14 at 06:41
  • Yes, since each src has a different rename prefix. You could potentially merge the streams after the rename pipe and use a single dest pipe, but I'm not sure the added complexity would provide any benefits. – Doodlebot Aug 27 '14 at 18:20