【问题标题】:grunt.file.copy exclude empty foldersgrunt.file.copy 排除空文件夹
【发布时间】:2014-01-26 21:31:40
【问题描述】:

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

这是我的任务

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

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

【问题讨论】:

  • 对,只是越来越模糊了。
  • 我刚刚意识到这些文件夹不是空的。
  • @IlanFrumer 对不起,我没有关注。
  • 我更新了我的解决方案

标签: gruntjs copy directory


【解决方案1】:

更新

我为 grunt-contrib-copy 创建了一个 PR,但 @shama 想出了一个更好的解决方案。

您现在可以使用过滤器在所有 grunt 任务中处理此问题:

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 :

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

【讨论】:

  • 我会看看这个。似乎有道理,但我已经被拉去做别的事情了。我可能有 ?等我回来的时候给你。谢谢
  • 嗨,a) 太棒了,感谢您的超快速转身。 b) 我如何获得这个版本的 grunt-contrib-copy?我的 project.json 文件中目前有“~0.5.0”。我想既然你刚刚这样做了,我应该更新那个版本?
  • 我的 PR 被拒绝了,因为 grunt 内置支持过滤所有插件的文件和文件夹。只需复制更新的 sn-p 即可。
  • 奇怪的是,这似乎不起作用。我会更新我的?完成我的繁重任务。
  • 感谢您的帮助。我认为第一个解决方案正在发生的事情是它正在查看源文件,而不是目标文件。源文件有文件,但复制的只是空文件夹,因为这些文件被过滤掉了。所以源有文件,因此被复制,但目标没有,因此很糟糕。无论如何,我也来到了你的第二个解决方案,我正在使用一个名为 grunt-cleanempty 的包,它可以工作,我只是希望在一项任务中完成它。再次感谢您的帮助。
猜你喜欢
  • 2015-06-11
  • 2013-08-03
  • 2017-10-12
  • 2013-07-17
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
相关资源
最近更新 更多