Arti
Arti

Reputation: 7762

grunt run multiple tasks in function

I'm using grunt-shell and other custom tasks. And in my task i want to run this tasks step by step and check what tasks return. For example, pseudo code:

grunt.task.registerTask('test', function () {
    grunt.log.header('Running app');
    var response = grunt.task.run('shell:go_to_folder');
    if (response == 'folder not found') {
        grunt.task.run('shell:create folder');
    } else {
        ...
    }

    grunt.log.ok('Done');
});

But grunt.task.run is async function, and didn't return any response or promise.

Upvotes: 0

Views: 586

Answers (1)

RobC
RobC

Reputation: 24952

You could consider using the grunt-shell custom-callback feature to capture the responses and handle any conditional logic.

The following example gist when running $ grunt via your command line will perform the following:

  1. Change directory to a folder named quux in the projects root directory. If the folder named quux exists the following will be logged:

    Done: Successfully changed to directory 'quux'

  2. However, if a folder named quux does not exist in the projects root directory the following will be logged:

The folder named 'quux' is missing

  1. Subsequently the shell:create_folder Task is run, which upon creating the folder named quux logs:

Successfully created folder named 'quux'

Finally the shell:go_to_folder Task is run, which then reports point 1 above.

Gruntfile.js

module.exports = function (grunt) {

  'use strict';

  grunt.loadNpmTasks('grunt-shell');

 /**
  * This callback is passed via the `go_to_folder` Task.
  * If the folder named `quux` is missing it is reported to the Terminal
  * and then invokes the `create_folder` Task.
  * However, if the folder named `quux` does exists it reports successfully
  * changing to the directory.
  */
  function goToFolderCB(err, stdout, stderr, cb) {

    // Check if the OS error reports the folder is missing:
    //  - `No such file or directory` is reported in MacOS (Darwin)
    //  - `The system cannot find the path specified` is reported in Windows cmd.exe
    if (stderr && stderr.indexOf('No such file or directory') > -1
        || stderr.indexOf('The system cannot find the path specified') > -1) {

      grunt.log.writeln('The folder named \'quux\' is missing');
      grunt.task.run('shell:create_folder');
    } else {
      grunt.log.ok('Done: Successfully changed to directory \'quux\'');
    }
    cb();
  }

 /**
  * This callback is passed via the `create_folder` task.
  * After reporting successfully creating the folder named `quux`
  * it runs the `go_to_folder` Task.
  */
  function createFolderCB(err, stdout, stderr, cb) {
    grunt.log.writeln('Successfully created folder named \'quux\'');
    grunt.task.run('shell:go_to_folder');
    cb();
  }

  grunt.initConfig({
    shell: {
      go_to_folder: {
        command: 'cd quux',
        options: {
          stderr: false, // Prevent stderr logs in Terminal.
          callback: goToFolderCB
        }
      },
      create_folder: {
        command: 'mkdir quux',
        options: {
          callback: createFolderCB
        }
      }
    }
  });

  grunt.task.registerTask('test', function () {
    grunt.log.header('Running app');
    grunt.task.run('shell:go_to_folder');
  });

  grunt.registerTask('default', ['test']);
};

Upvotes: 1

Related Questions