grunt-contrib-watch
grunt-contrib-watch copied to clipboard
Custom options not passed along to spawn()
I'm having a problem with a setup like this:
grunt.initConfig({
watch: {
less_templates: {
files: ["dev/styles/**/*.less"],
tasks: ["less"]
}
},
less: {
default: {
src: "dev/styles/app.less",
dest: "public/styles/<%= global.assets.css.app %>"
}
}
});
var timestamp = grunt.option("timestamp") || String((new Date()).getTime());
grunt.option("timestamp", timestamp);
grunt.config("global.assets.css.app", "app." + timestamp + ".css");
So as you can see, I generate a timestamp used to version assets and pass it along using grunt.option mechanism.
This works nice with other modules, however not with grunt-contrib-watch. Each spawned worker is started with fresh cli arguments, instead of the ones defined in the options hash. I believe the critical spot is here (in "tasks/lib/taskrun.js"):
self.spawned = grunt.util.spawn({
// Spawn with the grunt bin
grunt: true,
// Run from current working dir and inherit stdio from process
opts: {
cwd: self.options.cwd.spawn,
stdio: 'inherit',
},
// Run grunt this process uses, append the task to be run and any cli options
args: self.tasks.concat(self.options.cliArgs || []),
}/*, ...*/
);
So it seems you intentionally include only the cli options and ignore the grunt.option.flags()
part. Is this intentional? Or just a bug?
It should default to all cli args passed grabbed from process.argv
itself: https://github.com/gruntjs/grunt-contrib-watch/blob/master/tasks/lib/taskrunner.js#L160
I'd accept a patch that merged grunt.option.flags()
as well though. Thanks!
This bit me, too. I have to choose between disabling spawn (which means only one watch task runs, regardless of matched files) or allowing spawns without custom config properties, which I can't do.