grunt.file.copy排除空文件夹 [英] grunt.file.copy exclude empty folders

查看:165
本文介绍了grunt.file.copy排除空文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

file.copy(实际上是grunt-config-copy,但它使用grunt.file.copy下面)。这对我很好,但我排除某些glob模式。此排除会导致一些空文件夹,并且文件夹仍会复制到新集。有没有办法排除空文件夹?
谢谢,
Raif



这里是我的grunt任务

  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
}
}
}

日志显示返回了一些false值,但是我的desFolder中仍然有几个空文件夹。

解决方案

UPDATE < h3>

我做了一个公关<

现在,您可以在所有grunt任务中处理这个问题了。 filter:

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



一个好的解决方案是使用grunt-contrib-copy然后grunt-contrib-clean:

p>

  module.exports = function(grunt){
grunt.loadNpmTasks('grunt-contrib -清洁');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.initConfig({
copy:{
main:{
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']);
};


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.

解决方案

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

这篇关于grunt.file.copy排除空文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆