Search code examples
gulp

Use Gulp To Copy File Into Directory Named After Filename


I have the following files

assets
---views
------pages
---------some_page.ejs
---------another_page.ejs

When I build these ejs files, I want them to go into the following file structure

public
---some_page
------index.html
---another_page
------index.html

My gulp file contains the following build command:

gulp.task('build-html', function() {
return gulp.src('assets/views/pages/*.ejs')
    .pipe(ejs())
    .pipe(gulp.dest('public/???')); // WHAT DO I ADD HERE TO MAKE ABOVE WORK?
});

I'm totally stumped what I need to add into the gulp.dest function to achieve the above.


Solution

  • Solved with gulp-rename package:

    gulp.task('build-html', function() {
    return gulp.src('assets/views/pages/*.ejs')
        .pipe(ejs())
        .pipe(rename(function (path) {
           path.dirname += "/" + path.basename;
           path.basename = "index";
        }))
        .pipe(gulp.dest('public/'));
    });