简体   繁体   中英

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

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

    .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/'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM