grunt.file.copy exclude empty folders

故事扮演 提交于 2019-12-10 11:18:08

问题


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.


回答1:


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']);
};


来源:https://stackoverflow.com/questions/21001469/grunt-file-copy-exclude-empty-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!