Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt Command Line Parameters

Tags:

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?

like image 724
SL Dev Avatar asked Nov 21 '13 17:11

SL Dev


People also ask

How do I pass a parameter to a grunt task?

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".

What is the use of grunt cli?

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.


2 Answers

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'); 
like image 149
badsyntax Avatar answered Oct 08 '22 00:10

badsyntax


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']); } 
like image 27
Ricardo Avatar answered Oct 07 '22 23:10

Ricardo