CodezPrixx
CodezPrixx

Reputation: 21

Replace all files in Folder with 1 file but keeping their original names in Gulp

I have a folder that has bunch of images, i want gulp to replace each one of them with the new image file but without changing their original name

gulp.task('img', function() {
    return gulp.src(`source/template/img/**/*`, base: './')
    .pipe()// A pipe to replace multipe files with one file but keeping their original name
    .pipe(gulp.dest(`./`));
});

How can i do that?

Upvotes: 2

Views: 288

Answers (1)

Jim Factor
Jim Factor

Reputation: 1535

This makes use of vinyl-map to handle the pipe function. It also uses readFileSync as I couldn't get it to work async. If someone knows how to do so, please modify my answer or leave a comment/new answer.

MightyRiversbank.jpg is the template image that replaces all existing images in the tempImages directory.

const gulp = require('gulp')
const fs = require('fs')
const map = require('vinyl-map')
const gutil = require('gulp-util')

gulp.task('replaceImages', function() {
    const replaceImage = map(function(code, filename, callback){
      const data = fs.readFileSync('MightyRiversbank.jpg')
      return data
    })

    gutil.log("replacing images")
    return gulp.src(`tempImages/**/*`)
    .pipe(replaceImage)
    .pipe(gulp.dest(`tempImages`))
})

Upvotes: 1

Related Questions