0

I want to get const name from the terminal using grunt, and use it in uglify. This is what i want to happen:

    uglify: {
        options: {
          sourceMap: true,
          compress: {
               global_defs: {
                 <myConst>: false
             }
          }
        },
        ugly: {
            src: 'beautiful.js',
            dest: 'ugly.js'
        }
    }

I use:

grunt --target=blabla

to pass the parameter, so myConst should be the input from the terminal (in this case blabla). I can't seem to find a way to put it instead of myConst (in code). Is it possible and how can i do it?

Zbun
  • 4,875
  • 4
  • 19
  • 28

1 Answers1

1

Since running grunt gives you the following command-line arguments in process.argv:

  1. node
  2. path_to_grunt_script

Couldn't you simply do something like:

module.exports = function(grunt) {

    var compress_defs={},
        args=process.argv.slice(2); // take all command line arguments skipping first two

    // scan command line arguments for "--target=SOMETHING"
    args.forEach(function(arg){
        if(arg.match(/--target=(\S+)/)) { // found our --target argument
            compress_defs[RegExp.$1]=false;
        }
    });

    grunt.initConfig({
        uglify: {
            options: {
                sourceMap: true,
                compress: {
                    global_defs: compress_defs
                }
            },
            ugly: {
                src: 'beautiful.js',
                dest: 'ugly.js'
            }
    });
};

Or better yet, rather than rolling-your-own, use a command-line processing library like minimist.

Rob Raisch
  • 17,040
  • 4
  • 48
  • 58