home
Hero image for: Grunt How to list available tasks

Grunt How to list available tasks

By Eduardo García ● CTO | August 26th, 2014

In the blog entry Introduction to Grunt I explained how we could use Grunt to automate tasks, but the only way to detect the available tasks is to review the Gruntfile.js file or perform the tasks, but it's not a good idea execute tasks that do not know what they do.

Fortunately, there is a plugin that allows Grunt toe review tasks available, this plugin is grunt-available-tasks. Let's see how we can implement it in our project.

Install plugin.

The first thing to do is install the plugin available-tasks running assuming you have npm installed just execute the following command.

$ npm install grunt-available-tasks --save-dev

The above command will add the respective dependency in your package.json file and you will get a similar entry to the following file.

"grunt-available-tasks":"0.5.0"

Add a task to Grunt.

Having as a base the file Gruntfile.js created in previous entry “Introduction to Grunt” we will include the necessary configuration to add the task to list the available tasks as shown below.

availabletasks: {
  tasks: {
    options: {
      filter: 'exclude',
      tasks: ['availabletasks', 'tasks']
    }
  }              
},

The above action registers the configuration for the plugin available-tasks indicating that we want to exclude from the list of tasks “availabletasks” and “tasks “ the last one is an alias to configure later.

Load the plugin.

In order to execute the command availabletasks/tasks we have load the plugin with the following settings Gruntfile.js close the end of the file as shown in the following listing

grunt.loadNpmTasks('grunt-available-tasks');

Register the task.

Finally let's register the task using an alias to facilitate their implementation, for which we just add the following code at the end of the configuration file.

grunt.registerTask('tasks', ['availabletasks']);

Run the command

At this point, we could get the tasks available and should just run the following command.

$ grunt tasks

The above command would get an output similar to the following image.

grunt tasks

The image above was executed within the project Community Bookstore. See the full configuration file at https://github.com/enzolutions/community-bookstore/blob/master/frontend/Gruntfile.js.

I hope you have been to his liking.

Related Posts