Search code examples
gruntjsgrunt-contrib-watch

Pass in param to 'Grunt watch' for directory


I use grunt to less>css, minification, and concatenation of my CSS and JS. I am wondering if it's possible to pass in a param when you type in "grunt watch" which would be the directory to watch. That way I can have multiple versions of a site running off the same gruntfile.

http://pastebin.com/b2FJ74SC


Solution

  • You can use node's process.argv to get the command line arguments.

    The code below can be called with: grunt watch --dir myfolder

    grunt.registerTask('watch', function() {
        var args = process.argv;
        var folder = args[args.indexOf('--dir') + 1];
        console.log('watch folder: ', folder); // "watch folder: myfolder"
    });