Search code examples
node.jsnpmgulpzurb-foundationinky

gulp task that dynamically create folder with name based on file name


I have the following gulp task that is currently not working.

gulp.task('emails', function() {
  gulp.src('views/emails/src/**/*.html')
    .pipe(inky())
    .pipe(gulp.dest('views/emails/dist/'+debug()+"/html.ejs"));
});

I would like to iterate over the /views/emails/src/ directory, find all html files, then use inky to convert them to html, and then copy the resulting html file to...

views/emails/dist/'+ folderName +"/html.ejs

where folderName is the name of the .html file that was processed.

I need this in order to get the file structure in the format that the npm email-templates package requires.


Solution

  • That's a job for gulp-rename:

    var rename = require('gulp-rename');
    var path = require('path');
    
    gulp.task('emails', function() {
      gulp.src('views/emails/src/**/*.html')
        .pipe(inky())
        .pipe(rename(function(file) {
          file.dirname = path.join(file.dirname, file.basename);
          file.basename = 'html';
          file.extname = '.ejs';
        }))
        .pipe(gulp.dest('views/emails/dist/'));
    });