thugsb
thugsb

Reputation: 23426

Grunt watch to fire different tasks in different environments

After GruntJS generates my CSS+JS, I want it to copy the generated files to the appropriate places on the servers, and so I've set up different grunt-copy tasks to do so. The paths to copy to are different for the development and production environments:

        devjs: {
            src: paths.dest + '/assets/prod.min.js',
            dest: '/srv/www/dev/js/prod.min.js'
        },
        prodjs: {
            src: paths.dest + '/assets/prod.min.js',
            dest: '/srv/www/htdocs/js/prod.min.js'
        },

How can I get grunt watch to know which environment it's in, and so fire the appropriate grunt-copy tasks for that environment? Do I need to have a config file that isn't tracked in the git repo or something that defines the environment, or is there a better way to do so?

Currently my grunt-contrib-watch.js has the following, although I'd like it to only fire copy:devjs OR copy:prodjs depending on the environment.

        js: {
            files: [
                paths.assets + '/**/*.js',
            ],
            tasks: ['js', 'copy:devjs', 'copy:prodjs']
        },

Any ideas? Thanks!

Upvotes: 0

Views: 180

Answers (2)

thugsb
thugsb

Reputation: 23426

I chose to go with using command-line options for this. So I'd use e.g. grunt watch --enviro=prod on the command line (and cron), and then have this in grunt-contrib-watch.js:

module.exports = function(grunt) {

    var paths = grunt.config.get('paths');

    if ( grunt.option( "enviro" ) === 'prod' ) {
        var jsprocess = ['js', 'copy:prodjs'];
    } else if ( grunt.option( "enviro" ) === 'dev' ) {
        var jsprocess = ['js', 'copy:devjs'];
    } else {
        var jsprocess = ['js'];
    }

    grunt.config.set(
        'watch',
        {
            options: {
                expand: true
            },
            css: {
                files: [
                    paths.assets + '/**/*.*ss',
                ],
                tasks: cssprocess
            },...

Upvotes: 1

LifeQuery
LifeQuery

Reputation: 3282

How can I get grunt watch to know which environment it's in?

The best practice is to define a NODE_ENV environment variable to differentiate between environments. You could then use some clause using process.env.NODE_ENV to register different tasks.
You would then be able to run the same task name with different configs.
For example:

if(process.env.NODE_ENV === 'prod')
    grunt.registerTask('mytask', ['watch:prodjs']);
else
    grunt.registerTask('mytask', ['watch:devjs']);

A different approach (and a better one in my opinion), is to just register different tasks per environment.
This way is more explicit and thus safer.

grunt.registerTask('prod-task', ['watch:prodjs', 'some-other:prodtask']);
grunt.registerTask('dev-task', ['watch:devjs', 'some-other:devtask']);

Then you can just run whichever task you need for you environment.

grunt prod-task

Upvotes: 1

Related Questions