Is there a way to make the Grunt plugin crash if there are no input files?

I worked with the Grunt cssmin plugin . I had a block in my Gruntfile that looked something like this:

cssmin: { target: { files: { '<%= config.target %>/mytarget.css': [ 'bower_components/normalize.css/*.css', 'bower_components/html5-boilerplate/css/main.css', '<%= config.src %>/css/*.css' ] } } } 

For some time this worked fine; but I moved it to another machine and did not correctly configure my deck components - the html5-boilerplate/css/main.css missing - and yet the task was still completed successfully. In fact, I can put completely fake paths into this array, and minimization is still being completed.

Is there any way at all to make Grunt crash and exit if the files are missing from such an array? (I'm not sure if the file array is a general Grunt concept or provided by cssmin). If not, is there a way to cause this particular plugin to crash?

(By the way, I know that the HTML5 Boilerplate is probably a bit old-fashioned these days, but I'm in the process of moving the old site. I also fixed my Bower installation process so that it runs automatically before this step, but I still would like to figure out more general solution to the problem with missing files).

+5
source share
3 answers

In file array format, you can use nonull: true to nonull: true warning if the file does not exist:

 files: [ { src: [ 'a.js', 'b.js', ], dest: 'c.js', nonull: true } ] 

Now make Grunt dwell on the warning by including grunt.log.warn in your own function:

 var gruntLogWarn = grunt.log.warn; grunt.log.warn = function(error) { gruntLogWarn(error); // The original warning. grunt.fail.warn('Stop! Hammer time.'); // Forced stop. }; 

Above will stop at any warning. But we want to stop if the source files are missing. Filter the Grunt warnings to "Source file ... not found":

 var gruntLogWarn = grunt.log.warn; grunt.log.warn = function(error) { var pattern = new RegExp("^Source file (.*) not found.$"); if (pattern.test(error)) { grunt.fail.warn(error); } else { gruntLogWarn(error); } }; 
+5
source

You can create a task that precedes cssmin .

 grunt.task.registerTask('checkIfFilesExist','',function(){ // Check for those files here. Throw if something wrong. }); grunt.task.registerTask('checkBeforeDoingStuff',[ 'checkIfFilesExist', 'cssmin' ]); 

And iirc, you can reuse the same parameters in the cssmin task by simply passing them through <% %> %% <% %> .

+2
source

The Grunt nonull: true option should deal with this, but it does not always work with all tasks. For me, this works with htmlmin , phpmin but not cssmin and uglifyJS .

To cause an error if the file is missing, you can instead create a filter function.

 var checkFileExists = function(filepath) { if (!grunt.file.exists(filepath)) { grunt.fail.warn('Could not find file: ' + filepath); } else { return true; } }; { src: ['<%= meta.srcPath %>/index.php'], dest: '<%= meta.deployPath %>/index.php', filter: checkFileExists, nonull: true}, 

Using the above script, it now works great with every tested task.

0
source

Source: https://habr.com/ru/post/1210177/


All Articles