如果目录为空,我怎样才能跳过一个咕噜的任务? [英] How can I skip a grunt task if a directory is empty

查看:135
本文介绍了如果目录为空,我怎样才能跳过一个咕噜的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用grunt-contrib的 concat uglify 模块来处理一些javascript。目前,如果 src / js / 为空,它们仍然会创建一个(空)concat'd文件以及缩小版本和源映射。

I'm using grunt-contrib's concat and uglify modules to process some javascript. Currently if src/js/ is empty, they will still create an (empty) concat'd file, along with the minified version and a source map.

我想在执行之前检测 src / js / 文件夹是否为空,如果是,则该任务应该跳过(不失败)。任何想法如何做到这一点?

I want to task to detect if the src/js/ folder is empty before proceeding, and if it is, then the task should skip (not fail). Any ideas how to do this?

推荐答案

解决方案可能不是最漂亮的,但可以给你一个想法。你需要首先运行 npm install --save-dev glob 。这是基于您提到的 Milkshake 项目的一部分。

The solution may not be the prettiest, but could give you an idea. You'll need to run something like npm install --save-dev glob first. This is based on part of the Milkshake project you mentioned.

grunt.registerTask('build_js', function(){
  // get first task's `src` config property and see
  // if any file matches the glob pattern
  if (grunt.config('concat').js.src.some(function(src){
    return require('glob').sync(src).length;
  })) {
    // if so, run the task chain
    grunt.task.run([
        'trimtrailingspaces:js'
      , 'concat:js'
      , 'uglify:yomama'
    ]);
  }
});

比较要点: https://gist.github.com/kosmotaur/61bff2bc807b28a9fcfa

这篇关于如果目录为空,我怎样才能跳过一个咕噜的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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