I'm attempting to create a Jade task that will have a build and dev task that have very similar options, except a dev boolean, and a different destination. The simplest way I've been able to achieve this is:
jade: {
dev: {
options: {
data: {
dev: true, // dev true
config: ...,
pkg: ...,
helpers: ...
}
},
files: [{
dest: '<%= config.workingDir %>',
...
}]
},
build: {
options: {
data: { // no dev
config: ...,
pkg: ...,
helpers: ...
}
},
files: [{
dest: '<%= config.buildDir %>',
...
}]
}
}
There's considerable repetition in this though, particularly if I want to add more options down the track. So I'm trying to create one build task that will work both from command line and from a watch task.
The closest I've gotten is this setup, where I can run grunt jade --dev
from the command line but can't set the dev boolean in the watch task.
build: {
options: {
data: {
dev: '<%= grunt.option("dev") %>',
config: ...,
pkg: ...,
helpers: ...
}
},
files: [{
dest: '<%= grunt.option("dev") ? config.workingDir : config.buildDir %>',
...
}]
}
The watch task:
watch: {
jade: {
...
tasks: ['jade'] // the option is false
}
}
I've also attempted to create a custom task that sets the option and then runs watch, which when listening to the watch event I can see the option is correctly set
grunt.registerTask('dev', 'Grunt dev mode', function(){
grunt.option('dev', true);
grunt.task.run('watch');
});
grunt.event.on('watch', function(action, filepath, target) {
console.log(grunt.option('dev')); // true
});
Even though the log shows the dev boolean as true the wrong task options are being passed. So, with all that said, am I going about setting options the right way or am I just stuck with having a build and dev task that repeat the same information?
I've also tried using the grunt.util._.expand method with little success.
data: "<%= grunt.util._.extend(jade.options.data, {dev: true}) %>",