hparra / gulp-rename

Rename files easily
MIT License
692 stars 73 forks source link

Renaming directory maintain the original empty directory on destination #65

Closed giovannicandido closed 8 years ago

giovannicandido commented 8 years ago

gulp.dest keeps creating the original renamed empty directory's

Suppose I have:

.pipe(rename(function(file){
    if(file.dirname.contains(packageReplacements)){
      file.dirname = file.dirname.replace(packageReplacements, newPackage)
    }
}))
.pipe(gulp.dest('.'))

The destination will have the original directories empty, and the renamed ones with the files

I think the expected behaviour is to remove then

shinnn commented 8 years ago

For your purpose, let gulp.src ignore directories by using glob pattern and options.

(ref. https://github.com/hparra/gulp-rename/issues/59#issuecomment-152194264)

giovannicandido commented 8 years ago

Just to clarify, my task is:

gulp.task('run', function (done) {

  return gulp.src(__dirname + '/templates/**')  // Note use of __dirname to be relative to generator
    .pipe(gulpFilter(filterFiles))
    .pipe(template(answers,{
      interpolate: /<%=([\s\S]+?)%>/g
    }))                 // Lodash template support
    .pipe(rename(function(file){
      if(file.basename[0] === '@'){
        file.basename = '.' + file.basename.slice(1);
      }
      // packageName support
      var newPackage = answers.packageName.split('.').join('/');
      for(var i=0;i<packageReplacements.length;i++){
        // startsWith
        if(file.dirname.contains(packageReplacements[i])){
          file.dirname = file.dirname.replace(packageReplacements[i], newPackage)
        }
      }

    }))
    .pipe(gulpFilter(['**','!server/src/**/info','!server/src/**/info/**'])) // Work around to remove the empty packages
    .pipe(conflict('./'))                    // Confirms overwrites on file conflicts
    .pipe(gulp.dest('./'))                   // Without __dirname here = relative to cwd
    .pipe(install())                         // Run `bower install` and/or `npm install` if necessary
});

The point of interest is all directories with the pattern /info/atende/touch/ are renamed to /user/provided/directories/, but after the rename the directory is still on the stream (empty) and therefore gulp.dest creates then. So, another filter takes place after the rename and remove the renamed folders from the stream.

Is there a better way instead of using a filter after de rename? Feel like a hack but a can live with that :-)

shinnn commented 8 years ago

Is there a better way instead of using a filter

Just what I said in https://github.com/hparra/gulp-rename/issues/65#issuecomment-184060339. The simplest way is using glob's nodir option.

Further questions go to StackOverflow. http://stackoverflow.com/questions/tagged/gulp-rename