Is there a way to cause a Grunt plugin to fail if input files are missing?
In the files array format you can use nonull: true
to raise a warning if a file doesn't exist:
files: [
{
src: [
'a.js',
'b.js',
],
dest: 'c.js',
nonull: true
}
]
Now force Grunt to stop on a warning by wrapping 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 on any warning. But we want to stop if source files are missing. Filter Grunt warnings on '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);
}
};
You could create a task that goes before cssmin
.
grunt.task.registerTask('checkIfFilesExist','',function(){
// Check for those files here. Throw if something's wrong.
});
grunt.task.registerTask('checkBeforeDoingStuff',[
'checkIfFilesExist',
'cssmin'
]);
And iirc, you could reuse the same params in the cssmin task by just referring them via <% %>
.