Reputation: 5554
When you create a new task with grunt, ie:
grunt.registerMultiTask('name', 'description', function() {...});
A files object is made available inside the task function as this.files
. This is the normalized result of looking at the value passed to task.files
in the grunt config and parsing it, expanding any globbing.
The first problem is, I have a task which needs to recurse potentially several times. Each time, it wants to use the files object to get an uptodate list of all files etc matching the initial configuration parameters. Eg:
grunt.registerMultiTask('sometask', 'description', function sometask() {
var files = this.files;
//do some stuff with files, then run this func again if needbe:
if(somecondition) sometask.call(this);
});
The problem here is that the files object is not updated to reflect any changes that I have made to the file structure, so the next time the function is called, the files list is potentially out of date.
What I'd like to be able to do is something like:
grunt.registerMultiTask('sometask', 'description', function sometask(renormal) {
//update this.files if we need to:
if(renormal) {
this.files = renormalize(this.data.files);
}
var files = this.files;
//do some stuff with files, then run this func again if needbe:
if(somecondition_is_matched) sometask.call(this, renormal);
});
Further, perhaps I want to make a plugin that takes two lots of file mappings, so in the grunt config I might have something like:
grunt.initConfig({
...
someplugin: {
filesOne: [{
cwd: "hello"
src: ["something/**"],
dest: "another"
}],
filesTwo: {
"another2": ["soemthing2/*", "!something2/*.js"]
}
}
...
});
And in the plugin, I'd want to be able to normalize whatever filesOne
and filesTwo
is, just like grunt normalizes 'files', so that the user can input src-dest file mappings in all of the usual formats.
The grunt API seems to expose functions for expanding patterns etc, but does not seem to provide anything to achieve the normalisation it does under the hood with the files object.
Is there something I'm missing/some accepted way to do this?
Upvotes: 3
Views: 183
Reputation: 549
There is actually a method grunt.task.normalizeMultiTaskFiles
:
grunt.registerMultiTask('sometask', 'description', function() {
var filesOne = grunt.task.normalizeMultiTaskFiles(
{files: this.data.filesOne}, this.target);
var filesTwo = grunt.task.normalizeMultiTaskFiles(
{files: this.data.filesTwo}, this.target);
})
Upvotes: 1