Use grunt.option in task config via grunt.template

To just ask this question, I'm looking for a way to pass a command line argument similar to the following that can be used in my task's config files.

$ grunt mytask --site=abcd

      

To detail my setup, I'm using load-grunt-config, so my / Gruntfile.js only contains the following ...

module.exports = function (grunt) {
   "use strict";
   require("load-grunt-config")(grunt);
};

      

Then I have a / grunt folder that has files like /grunt/compass.js,/grunt/aliases.js,/grunt/watch.js, etc., where each file is the configuration for this task. Here's an example of my /grunt/watch.js file:

module.exports = {
    compass: {
        files: ['ui/scss/**/*.scss'],
        tasks: ['compass:local']
    }
};

      

Now what I want to do is something like the following. I would like to use the grunt.template format to input the value that I am passing from the command line. It doesn't have to be in grunt.option format as I could read the value somewhere else and assign it to the / config / etc variable, but I just can't figure out how to get this command line parameter in my config.

module.exports = {
    compass: {
        files: ['ui/scss/*<%= grunt.option("site") %>*.scss'],
        tasks: ['compass:local']
    }
};

      

My main problem is that I cannot figure out how to get the <% = grunt.option ("site")%> code above to work. (Or something similar, perhaps <% = grunt.config.site%> )

Inside / Gruntfile.js I can read the option with the following ...

module.exports = function (grunt) {
   "use strict";
   grunt.log.writeln(grunt.option("site"));
   require("load-grunt-config")(grunt);
};

      

... but from there, I can't figure out how to put this value somewhere that the task config can read it. Any ideas? Any help is appreciated! Thank.

+3


source to share


1 answer


Probably a more elegant way to do this, maybe something in v.1.0.0

, but I'm currently on a previous stable release v.0.4.5

.

If you want to access grunt.option("whatever")

in your lodash templates <%= %>

you need to set it in the property grunt.initConfig({})

.

Let's say you want to have a flag --prod

on startup grunt build

to determine if it doesn't work.

You can install:



grunt.initConfig({

    ...
    production: grunt.option('prod'),
    ...

      

and then you can use it in your templates as such:

templates: {
            ...
            dest: '<%= production ? paths.dist : paths.dev %>/'
        }

      

So when you run grunt build --prod

, the destination folder goes to the right place.

0


source







All Articles