Andzej Maciusovic
Andzej Maciusovic

Reputation: 4476

Grunt run task with override config

I always used gulp for all my tasks automation, but in our current project we use grunt and I can't figure out how to make very simple thing:

grunt.initConfig({
    watch: {
        scripts: {
            files: '**/*.js',
            tasks: ['karma:watch:run'],
        }
    },
    myWatch: {
        scripts: {
            files: '**/*.js',
            tasks: ['do_my_own_task_but_not_karma'],
        }
    }
});

//current task
grunt.registerTask('default', ['watch']);

//doesn't work
grunt.registerTask('myWatchTask', ['myWatch']);

Basically I just want to override current watch config for my own custom task, because I don't want karma to run tests each time I change js file.

Upvotes: 0

Views: 446

Answers (1)

ed.
ed.

Reputation: 2706

You need to add an new configuration within the watch configuration object:

grunt.initConfig({
  watch: {
    main: {
      scripts: {
          files: '**/*.js',
          tasks: ['karma:watch:run'],
      }
    },
    myWatch: {
      scripts: {
          files: '**/*.js',
          tasks: ['do_my_own_task_but_not_karma'],
      }
  }
},
});

Then you can run grunt watch:main or grunt watch:myWatch, see these docs.

If you want to have something that is watched always you can add it to the root of the watch configuration object.

grunt.initConfig({
  watch: {
    files: '**/always.js',
    tasks: ['always'],
    main: {
      scripts: {
          files: '**/*.js',
          tasks: ['karma:watch:run'],
      }
    },
    myWatch: {
      scripts: {
          files: '**/*.js',
          tasks: ['do_my_own_task_but_not_karma'],
      }
  }
},
});

now grunt watch:myWatch will run it's dedicated config and the config in the root of the watch configuration object.

Upvotes: 1

Related Questions