How to Iterate multiple folders/files in Gulp

Gulp JavaScript

When searching for a method to loop not just the first level of files and folders, but also all the levels below. Asking to much I wanted the files name, its folder location and also the files content. I found it…

I found the Gulp plugin ‘src’ and ‘tap’…

To use the gulp-tap simply use the command ‘gulp’, use the ‘src’ property with wildcards for the folders/files and then add the ‘tap’ property on.

The ‘src’ is the part that gets the collection of the files and the folders, which can be direct to a file or using the wildcards you can iterate through the folders to multiple levels. There is then further conditional methods to filter the files and their types.

The ‘tap’ then taps into each of the returned items with its details. I was looking for the files path, name and the content, but the file object has much more to offer.

gulp.src("src/**/*.{json,config}")    .pipe(tap(function(file, t) {        // Files path and name e.g. src/folder/folder/package.json        var FilePathAndName = file.path;        // File path using the above to trim off the name
        Var FilePath = FilePathAndName.substring(0,FilePathAndName.lastIndexOf('\\');        // File name using the above to trim off the path
        Var FileName = FilePathAndName.substring(FilePathAndName.lastIndexOf('\\');        // Content of the file in string format
        Var FileContent = file.content;    }))Don’t forget to add the name into the package.json and the require command at the top of the JavaScript file as per the links below.

Gulp for Beginners >

Published by Chris Pateman - PR Coder

A Digital Technical Lead, constantly learning and sharing the knowledge journey.

Leave a message please

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: