Kaane Guji
Kaane Guji

Reputation: 125

Moving files from multiple directories using Gulp copies the directories as well

I'm setting up gulp to move all my html files from different modules into one views folder. Inside modules folder each module folder has the same structure

modulename/client/views/anyFile.html

I've written the gulp task 'move-html'.

gulp.task('move-html', function(){
    return gulp.src('./modules/*/client/views/*.html')
    .pipe(htmlmin({ collapseWhitespace: true }))
    .pipe(gulp.dest('./public/views'));
});

I expected all the html files from all the modules to be copied to ./public/views however, I found the entire module structures copied instead.

Expected: index.html

Actual output: app |-client |-views |-index.html

I'm guessing it's to do with the asterisk after after module/ but can't find any answer of an alternative way.

When I use a file name in gulp.src it works fine e.g. ./modules/app/client/views/*.html

Upvotes: 1

Views: 71

Answers (1)

Mark
Mark

Reputation: 181010

You are right that the portion of the path preceding the * is important here. It will form the base of the path. But that is not what you want. You want to eliminate parts of the path that include and follow the *. The easiest way to do that is with gulp-flatten, a package which will remove specified parent directories.

In your case you can simply remove them all like this:

.pipe(flatten())  

leaving only the filenames themselves, which you can then puit into any directories you want. So:

const flatten = require("gulp-flatten");

gulp.task('move-html', function(){
  return gulp.src('./modules/**/client/views/*.html')
    .pipe(htmlmin({ collapseWhitespace: true }))

    .pipe(flatten())

    .pipe(gulp.dest('./public/views'));
});

Now you will end up with ./public/views/*.html as you wanted.

Upvotes: 2

Related Questions