Sharikov Vladislav
Sharikov Vladislav

Reputation: 7279

How to run asynchrounously 2 sync task with Grunt?

I have 2 Grunt tasks: A and B. Exec time of A is 5 seconds. Exec time of B is 10 seconds.

I run this two tasks in pre-commit script. Both are sync, so pre-commit script exec time is 15. I want to somehow combine this two tasks and run them asyncrounously. So this will take only 10 seconds to execute pre-commit.

Is it possible?

This hell doesn't work :)

grunt.registerTask('pre-commit', 'Run sonar and jscs async', function() {
  var done = this.async(),
    _counter = 0,
    asyncTasksCounter = 2,
    asyncHelper = function (foo) {
      setTimeout(function () {
        foo();
        _counter++;
        if (_counter === asyncTasksCounter) {
          done();
        }
      }, 16.66)
    };
  asyncHelper(function () {
    grunt.task.run('A');
  });
  asyncHelper(function () {
    grunt.task.run('B');
  });
});

Upvotes: 0

Views: 60

Answers (1)

Andy
Andy

Reputation: 63587

I wouldn't use Grunt for this. Assuming you're using npm I would define the tasks separately and then use npm to run them using something like shell-executor (Windows and Mac).

After installing you add the tasks to the script section of package.json then run the script with npm run [scriptname], like npm run prepublish.

scripts: {
  "precommit": "shell-exec \"grunt sonar\" \"grunt jscs\"",
}

There's a similar script concurrently which works well for concurrent tasks.

The reason these modules are necessary is because the varying OSes handle & (parallel) and && (concurrent) differently, otherwise you could do:

"precommit": "grunt sonar & grunt jscs",

Upvotes: 1

Related Questions