Question

file.copy (actually grunt-config-copy but it uses grunt.file.copy underneath). This is working fine for me but I'm excluding certain glob patterns. This exclusion results in some empty folders, and the folders are still copied to new set. Is there any way to exclude empty folders? Thanks, Raif

here is my grunt-task

copy: {            
        release: {
                expand: true,
                deleteEmptyFolders:true,
                cwd:'<%= srcFolder %>',
                src: ['**',
                      '!**/obj/**',
                      '!**/*.cs',
                      '!**/*.vb',
                      '!**/*.csproj',
                      '!**/*.csproj.*'
               ],
               dest: '<%= destFolder %>',
               filter: function(filepath) {
                 var val = !grunt.file.isDir(filepath) || require('fs').readdirSync(filepath).length > 0;
                 grunt.log.write(val);
                 return val
              }
            }              
    }

the log shows that there are some false values being returned but I still have several empty folders in my desFolder.

Was it helpful?

Solution

UPDATE

I made a PR to grunt-contrib-copy but @shama came up with a better solution.

You can now handle this in all grunt tasks using filter:

copy: {
  main: {
    src: 'lib/**/*',
    dest: 'dist/',
    filter: function(filepath) {
      return ! grunt.file.isDir(filepath) || require('fs').readdirSync(filepath).length > 0;
    },
  },
},

The problem with your case is that those folders are not empty just being excluded.

A good solution would be to use grunt-contrib-copy and then grunt-contrib-clean :

module.exports = function(grunt) {
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.initConfig({
    copy: {
      main: {
        expand: true,
        cwd: 'src',
        src: ['**', '!**/*.js'],
        dest: 'dist/'
      }
    },
    clean: {
      main: {
        src: ['*/**'],
        filter: function(fp) {
          return grunt.file.isDir(fp) && require('fs').readdirSync(fp).length === 0;
        }
      }
    }
  });
  grunt.registerTask('copyclean', ['copy:main', 'clean:main']);
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top