I have a Grunt build file. My build file has a task that looks like the following:
myTask: { options: { configFile: "config/default.js", args: { } }, dev: { configFile: 'config/local.js', options: { args: {} }, }, test: { configFile: 'config/remote.js', options: { args: {} } } }
...
grunt.registerTask('customTask', ['myTask:dev']); grunt.registerTask('customTask-Test', ['myTask:test']);
Currently, I can run the following from the command line:
> grunt customTask
Everything works fine. However, I need to add the ability to do something like this:
> grunt customTask --myParam=myValue
I need to look at the value of myParam in my "dev" task target. However, I can't figure out how to do it. I would be happy if I could just print out the value of myParam when myTask:dev is ran. In other words, I'd like to see the following when run
> grunt customTask > grunt customTask --myParam=hello You entered hello > grunt customTask-Test > grunt customTask-Test --myParam=hello
How do I do something like this?
Another way to share a parameter across multiple tasks would be to use grunt. option . In this example, running grunt deploy --target=staging on the command line would cause grunt. option('target') to return "staging".
The CLI looks for the installed Grunt on your system by using require() system whenever Grunt is run. Using grunt-cli, you can run Grunt from any directory in your project. If you are using locally installed Grunt, then grunt-cli uses locally installed Grunt library and applies the configuration from the Grunt file.
This is all explained in the grunt.option page.
In your case, you could get the value of myParam
with:
var target = grunt.option('myParam');
I made an example of use, where i can pass the module where i want my css.min to be created through this command line:
> grunt cssmin --target=my_module
Gruntfile.js
module.exports = function(grunt) { var module = grunt.option('target'); //get value of target, my_module var cssminPath = 'assets/' + module + '/css/all.css'; grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), cssmin:{ css: { files: [{ src: [ 'bower_components/bootstrap/dist/css/bootstrap.min.css', ], dest: cssminPath }] } }, }); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.registerTask('default', ['cssmin']); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With