hparra/gulp-rename

Rename only files not folders

st-schneider opened this issue · 4 comments

I have a glob like /themes/**/public/** where I want to rename the files by eliminating the public folder.
Currently I have to go

return gulp.src('/themes/**/public/**')
    .pipe(rename((filepath: rename.ParsedPath) => {
      if (filepath.extname == '') {
        filepath.basename = filepath.basename.replace(/public\/?/, "");
      }
      filepath.dirname = filepath.dirname.replace(/public\/?/, "");
    }))
    .pipe(gulp.dest('target'));

It is a bit cumbersome to have to rename empty folders as well. If I leave out the if I end up with and empty public folder in the dest.
Is there a way to only affect files and not folders?

Maybe this:

return gulp.src('/themes/**/public/**', { nodir: true })
    ...

https://github.com/isaacs/node-glob#options

nodir Do not match directories, only files. (Note: to match only directories, simply put a / at the end of the pattern.)

We should pass the actual file in as a second arg so we can utilize this https://github.com/gulpjs/vinyl#fileisdirectory or other checks (maybe you want to rename based on contents? who knows)

Thank you

At least for this use case, @heikki's solution is most suitable.