Last Updated: February 25, 2016
·
6.566K
· denisonluz

Creating a '--force' task in Grunt

Sometimes you might need to use '--force' with Grunt when building or testing your app .
If you use this option like this for example "grunt server --force", the --force option will be applied to all Grunt tasks.

But normally you would like to apply the '--force' option for some grunt tasks but leave other tasks in the sequence without the '--force' option.

The way I do this is 'wrapping' the task I want to use '--force' between two other tasks that will set the '--force' option ON and OFF.

Here it is the code:

grunt.registerTask('forceOn', 'turns the --force option ON',
    function() {
      if ( !grunt.option( 'force' ) ) {
        grunt.config.set('forceStatus', true);
        grunt.option( 'force', true );
      }
    });

  grunt.registerTask('forceOff', 'turns the --force option Off',
    function() {
      if ( grunt.config.get('forceStatus') ) {
        grunt.option( 'force', false );
      }
    });

You can then wrap the task you want to use '--force' with these two tasks, like this:

grunt.task.run([
  'clean:server',
  'replace',
  'forceOn',  <-- turn the --force ON
  'concurrent:server',  <-- this task will use the --force option 
  'forceOff', <-- turn the --force OFF
  'connect:livereload', <-- all the remaining tasks won't use --force
  'open',
  'watch'
 ]);

1 Response
Add your response

exactly what i searched for! thanks!

over 1 year ago ·