hparra / gulp-rename

Rename files easily
MIT License
692 stars 73 forks source link

Rename only files not folders #78

Closed st-schneider closed 6 years ago

st-schneider commented 7 years ago

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?

heikki commented 7 years ago

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.)

yocontra commented 7 years ago

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)

st-schneider commented 7 years ago

Thank you

shinnn commented 6 years ago

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