How can I prefix a file name with it's parent directory using Gulp?

1.4k views Asked by At

I am using the following hierarchy for a project:

src/img/de-de/file1.jpg, file2.gif....
src/img/en-gb/file1.jpg...
src/img/it-it/etc

There are global images inside of the img folder, and localised files in each of the mentioned sub-directories.

I am using the gulp-rename plugin to add a custom prefix (GulpTest_) to file names. However, I also want to add the sub-directory folder name to any localised files that are stored inside.

Example:
src/img/de-de/file1.jpg would be renamed GulpTest_de-de_file1.jpg

I am currently using the following code to source each of the files in the sub-directories and add the GulpTest prefix:

// Image Rename Task
gulp.src('.src/img/*/*')
.pipe(rename({prefix: "GulpTest"}))
.pipe(gulp.dest("./dst/" + )

How can I amend my task so that it it concatenates 'prefix + sub-directory + file.jpg'?

Thanks

1

There are 1 answers

1
Leguest On BEST ANSWER
    .pipe(rename(function (path) {
        console.log('path', path);
        if (path.extname) {
            if (path.dirname === '.') {
                path.dirname = '';
            }
            path.basename = 'GulpTest_' + path.dirname + '_' + path.basename;
            path.dirname = '';
            console.log('path-new', path);
        }
    }))
    .pipe(gulp.dest('dst/'));