如何在命令行上获取grunt任务名称?

pas*_*seg 17 gruntjs

为了自定义我的grunt任务,我需要在启动grunt时访问命令行上给出的grunt任务名称.

选项没有问题,因为它有很好的文档(grunt.options).在运行grunt任务时,如何找到任务名称也有详细记录.

但我之前需要访问任务名称.

例如,用户写道 grunt build --target=client

在我的配置grunt作业时Gruntfile.js,我可以 grunt.option('target')用来获取'client'.

但是如何build在任务构建开始之前获取参数?

任何指导都非常感谢!

dc5*_*dc5 28

你的grunt文件基本上只是一个函数.尝试将此行添加到顶部:

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

// Add your pre task code here...
Run Code Online (Sandbox Code Playgroud)

运行时grunt build --target=client应该给你输出:

client
[ 'build' ]
Run Code Online (Sandbox Code Playgroud)

此时,您可以在运行任务之前运行所需的任何代码,包括使用新依赖项设置值.