manohar_tn
manohar_tn

Reputation: 101

how to use grunt target

My Gruntfile.js is something like:

 'use strict';

module.exports = function(grunt) {
    grunt.initConfig({
        grunt.config.set('targ', grunt.option('target'));
        cachebuster: {
            build: {
                options: {
                    basedir: 'WebContent',
                    formatter: function(hashes) {
                        var json = {};
                        for (var filename in hashes) {
                            json["/" + filename] = hashes[filename];
                        }
                        return JSON.stringify(json);
                    }
                },
                src: [ 'WebContent/assets/**/*.js', 'WebContent/assets/**/*.css' ],
                dest: 'src/main/resources/cachebusters.json'
            }
        }
    });

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-cachebuster');    

// Default task.
grunt.registerTask('default', ['cachebuster']);

};

I would like to change the location of dest based on dev or deploy command line argument..

i.e if command line arg is dev then dest : 'cachebuster.json' if command line arg is deploy then dest : 'src/main/resources/cachebuster.json'

How do i achieve this ??

Upvotes: 0

Views: 165

Answers (1)

Luuk Moret
Luuk Moret

Reputation: 506

You can try the following code blocks. By default the first target will get executed (dev in this case) if you supply no arguments:

cachebuster: {
  dev: {
    options: {
      basedir: 'WebContent',
      formatter: function(hashes) {
        var json = {};
        for (var filename in hashes) {
          json["/" + filename] = hashes[filename];
        }
        return JSON.stringify(json);
      }
    },
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'],
    dest: 'src/main/resources/cachebusters.json'
  },
  deploy: {
    options: {
      basedir: 'WebContent',
      formatter: function(hashes) {
        var json = {};
        for (var filename in hashes) {
          json["/" + filename] = hashes[filename];
        }
        return JSON.stringify(json);
      }
    },
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'],
    dest: 'src/main/resources/cachebusters.json'
  }
}

Use the following code block if you want to share the options across targets.

cachebuster: {
  options: {
    basedir: 'WebContent',
    formatter: function(hashes) {
      var json = {};
      for (var filename in hashes) {
        json["/" + filename] = hashes[filename];
      }
      return JSON.stringify(json);
    }
  },
  dev: {
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'],
    dest: 'src/main/resources/cachebusters.json'
  },
  deploy: {
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'],
    dest: 'src/main/resources/cachebusters.json'
  }
}

Upvotes: 2

Related Questions