How can I skip a grunt task if a directory is empty

前端 未结 3 587
無奈伤痛
無奈伤痛 2021-02-08 23:30

I\'m using grunt-contrib\'s concat and uglify modules to process some javascript. Currently if src/js/ is empty, they will still create a

3条回答
  •  故里飘歌
    2021-02-08 23:52

    Maybe this is just a more up-to-date answer as the others are more than a year old, but you don't need a plugin for this; you can use grunt.file.expand to test if files matching a certain globbing pattern exist.

    Update of @Kosmotaur's answer (path is just hard-code here though for simplicity):

    grunt.registerTask('build_js', function(){
      // if any file matches the glob pattern
      if (grunt.file.expand("subdir/**/*.js").length) { /** new bit here **/ 
        // if so, run the task chain
        grunt.task.run([
            'trimtrailingspaces:js'
          , 'concat:js'
          , 'uglify:yomama'
        ]);
      }
    });
    

提交回复
热议问题