Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get grunt task name given on command line?

Tags:

gruntjs

In order to customize my grunt tasks, I need access to the grunt task name given on the command line when starting grunt.

The options is no problem, since its well documented (grunt.options). It's also well documented how to find out the task name, when running a grunt task.

But I need access to the task name before.

Eg, the user writes grunt build --target=client

When configuring the grunt job in my Gruntfile.js, I can use grunt.option('target') to get 'client'.

But how do I get hold of parameter build before the task build starts?

Any guidance is much appreciated!

like image 653
paseg Avatar asked Jun 11 '13 10:06

paseg


People also ask

How do you check if task exists in Grunt?

grunt.task.existsCheck with the name, if a task exists in the registered tasks.

How do I download Grunt from command line?

Installing the CLI. Run sudo npm install -g grunt-cli (Windows users should omit "sudo ", and may need to run the command-line with elevated privileges). The grunt command-line interface comes with a series of options. Use grunt -h from your terminal to show these options.

Which statement will run all default task in Grunt when we run Grunt command?

For instance, when you define a taskList with jshint, concat, and uglify tasks and specify the taskName as default, all the listed tasks will be run automatically if Grunt is executed without specifying any tasks. grunt. registerTask('dist', ['concat:dist', 'uglify:dist']);

What is Grunt config?

config. Access project-specific configuration data defined in the Gruntfile . Note that any method marked with a ☃ (unicode snowman) is also available directly on the grunt object, and any method marked with a ☆ (white star) is also available inside tasks on the this object.


2 Answers

Your grunt file is basically just a function. Try adding this line to the top:

module.exports = function( grunt ) {
/*==> */    console.log(grunt.option('target'));
/*==> */    console.log(grunt.cli.tasks);

// Add your pre task code here...

Running with grunt build --target=client should give you the output:

client
[ 'build' ]

At that point, you can run any code you need to before your task is run including setting values with new dependencies.

like image 70
dc5 Avatar answered Oct 07 '22 12:10

dc5


A better way is to use grunt.task.current which has information about the currently running task, including a name property. Within a task, the context (i.e. this) is the same object. So . . .

grunt.registerTask('foo', 'Foobar all the things', function() {
  console.log(grunt.task.current.name); // foo
  console.log(this.name); // foo
  console.log(this === grunt.task.current); // true
});

If build is an alias to other tasks and you just want to know what command was typed that led to the current task execution, I typically use process.argv[2]. If you examine process.argv, you'll see that argv[0] is node (because grunt is a node process), argv[1] is grunt, and argv[2] is the actual grunt task (followed by any params in the remainder of argv).

EDIT:

Example output from console.log(grunt.task.current) on [email protected] from within a task (can't have a current task from not a current task).

{
  nameArgs: 'server:dev',
  name: 'server',
  args: [],
  flags: {},
  async: [Function],
  errorCount: [Getter],
  requires: [Function],
  requiresConfig: [Function],
  options: [Function],
  target: 'dev',
  data: { options: { debugPort: 5858, cwd: 'server' } },
  files: [],
  filesSrc: [Getter]
}
like image 30
tandrewnichols Avatar answered Oct 07 '22 11:10

tandrewnichols