如何在命令行中获取Grunt任务名称?

19
为了自定义我的Grunt任务,我需要访问在启动Grunt时在命令行中给出的Grunt任务名称。
选项并不是问题,因为它有很好的文档记录(grunt.options)。当运行Grunt任务时,也很容易找到任务名称(when running a grunt task)。
但是,在任务开始之前,我需要访问任务名称。
例如,用户写入 grunt build --target=client
在我的Gruntfile.js中配置Grunt作业时,我可以使用grunt.option('target')来获取'client'。
但是,在build任务开始之前,如何获得参数build的值?
非常感谢您的任何指导!
3个回答

30

你的grunt文件基本上只是一个函数。尝试在顶部添加这行:

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

// Add your pre task code here...

使用grunt build --target=client命令可以让你得到以下输出结果:

client
[ 'build' ]

在那时,您可以运行需要的任何代码,包括使用新依赖项设置值,然后再运行您的任务。


现在这个答案已经被编辑过了,是正确的。要查看从命令行给出的任务名称,请使用:console.log(grunt.cli.tasks); tasks 是一个数组。 - Zane

4
更好的方法是使用grunt.task.current,它包含有关当前正在运行的任务的信息,包括一个name属性。在任务内部,上下文(即this)是相同的对象。因此...
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
});

如果build是其他任务的别名,你只想知道导致当前任务执行的命令是什么,我通常使用process.argv[2]。如果你检查process.argv,你会发现argv[0]node(因为grunt是一个node进程),argv[1]grunt,而argv[2]是实际的grunt任务(后跟argv中的任何参数)。
编辑:
在grunt@0.4.5中从任务内部输出console.log(grunt.task.current)的示例输出(不能从非当前任务获得当前任务)。
{
  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]
}

grunt.task.current在Grunt 0.4.5中返回了undefined。你使用的是哪个版本? - theUtherSide
0.4.5. 你是在任务内部执行吗?如果不是,那么“未定义”就有意义了。我会将我的输出添加在上面(对于评论来说太乱了)。 - tandrewnichols
谢谢澄清,我在 module.exports = function (grunt) {... 里面,但是在 grunt.initConfig... 外面。看起来 grunt.task.current 在那里不可用。 - theUtherSide

1
您可以使用grunt.util.hooker.hook来实现此功能。
示例(Gruntfile.coffee的一部分):
grunt.util.hooker.hook grunt.task, (opt) ->
  if grunt.task.current and grunt.task.current.nameArgs
    console.log "Task to run: " + grunt.task.current.nameArgs

CMD:

C:\some_dir>grunt concat --cmp my_cmp
Task to run: concat
Running "concat:coffee" (concat) task
Task to run: concat:coffee
File "core.coffee" created.

Done, without errors.

我还使用了一个技巧来防止执行某些任务:

grunt.util.hooker.hook grunt.task, (opt) ->
  if grunt.task.current and grunt.task.current.nameArgs
    console.log "Task to run: " + grunt.task.current.nameArgs
    if grunt.task.current.nameArgs is "<some task you don't want user to run>"
      console.log "Ooooh, not <doing smth> today :("
      exit()  # Not valid. Don't know how to exit :), but will stop grunt anyway

CMD,当允许时

C:\some_dir>grunt concat:coffee --cmp my_cmp
Running "concat:coffee" (concat) task
Task to run: concat:coffee
File "core.coffee" created.

Done, without errors.

CMD,当被阻止时

C:\some_dir>grunt concat:coffee --cmp my_cmp
Running "concat:coffee" (concat) task
Task to run: concat:coffee
Ooooh, not concating today :(
Warning: exit is not defined Use --force to continue.

Aborted due to warnings.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接