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

系统教程 行业动态 更新时间:2024-06-14 16:57:39
如果目录为空,我怎样才能跳过一个咕噜的任务?(How can I skip a grunt task if a directory is empty)

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

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

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.

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项目。

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

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

A gist for comparison: https://gist.github.com/kosmotaur/61bff2bc807b28a9fcfa

更多推荐

本文发布于:2023-04-13 12:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/30023be7384fc4514b71ded1fe224c00.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳过   为空   目录   skip   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!