简体   繁体   English

以编程方式设置grunt任务的选项?

[英]Programmatically set options for grunt task?

I have a grunt task that looks at options with grunt.option('foo') . 我有一个grunt任务,用grunt.option('foo')查看选项。 If I'm calling this task from grunt.task.run('my-task') , how can I change those arguments? 如果我从grunt.task.run('my-task')调用此任务, grunt.task.run('my-task')该如何更改这些参数?

I'm looking for something like: 我正在寻找类似的东西:

grunt.task.run('my-task', {foo: 'bar'});

which would be the equivalent of: 这将相当于:

$ grunt my-task --foo 'bar'

Is this possible? 这可能吗?

( This question is another issue I ran in to but is not exactly the same, because in this case I don't have access to the original task's Gruntfile.js.) 这个问题是我遇到的另一个问题,但并不完全相同,因为在这种情况下我无法访问原始任务的Gruntfile.js。)

如果您可以使用基于任务的配置选项而不是grunt.option,那么这应该可以为您提供更精细的控制:

grunt.config.set('task.options.foo', 'bar');

Looks like I can use the following: 看起来我可以使用以下内容:

grunt.option('foo', 'bar');
grunt.task.run('my-task');

It feels a bit odd to set the options globally instead of just for that command, but it works. 设置全局选项而不仅仅是针对该命令感觉有点奇怪,但它有效。

Create a new task which set the option, then call the modified task. 创建一个新任务,设置该选项,然后调用修改后的任务。 This is a real life example with assemble : 这是汇编的真实例子:

grunt.registerTask('build_prod', 'Build with production options', function () {
  grunt.config.set('assemble.options.production', true);
  grunt.task.run('build');
});

In addition to @Alessandro Pezzato 除了@Alessandro Pezzato

Gruntfile.js: Gruntfile.js:

grunt.registerTask('build', ['clean:dist', 'assemble', 'compass:dist', 'cssmin', 'copy:main']);

    grunt.registerTask('build-prod', 'Build with production options', function () {
        grunt.config.set('assemble.options.production', true);
        grunt.task.run('build');
    });

    grunt.registerTask('build-live', 'Build with production options', function () {
        grunt.option('assemble.options.production', false);
        grunt.task.run('build');
    });

Now you can run 现在你可以跑了

$ grunt build-prod

-OR- -要么-

$ grunt build-live

They will both do the full task 'build' and respectively pass a value to one of the options of assemble , namely production 'true' or 'false'. 他们都将执行完整任务'build'并分别将值传递给汇编选项之一 ,即生产'true'或'false'。


In addition to illustrate the assemble example a bit more: 除了说明组装示例之外:

In assemble you have the option to add a {{#if production}}do this on production{{else}}do this not non production{{/if}} 在汇总时,您可以选择添加{{#if production}}do this on production{{else}}do this not non production{{/if}}

grunt is all programmatic.. so if you have set options on tasks before, you have done this programmatically. grunt是所有程序化的..所以如果您之前已经设置了任务选项,那么您已经以编程方式完成了此操作。

just use grunt.initConfig({ ... }) to set options for tasks. 只需使用grunt.initConfig({ ... })来设置任务选项。

and if you already initialized, and need to change configuration afterwards, you can do something like 如果你已经初始化,并且之后需要更改配置,你可以做类似的事情

grunt.config.data.my_plugin.goal.options = {};

I am using it for my project and it works. 我正在将它用于我的项目并且它有效。

I recently ran up against this same issue: programmatically setting grunt options and running tasks multiple times from within a single parent task. 我最近遇到了同样的问题:以编程方式在单个父任务中多次设置grunt选项和运行任务。 As @Raphael Verger mentions, this is not possible, as grunt.task.run defers the running of the task until the current task is finished: 正如@Raphael Verger所提到的,这是不可能的,因为grunt.task.run将任务的运行推迟到当前任务完成:

grunt.option('color', 'red');
grunt.task.run(['logColor']);
grunt.option('color', 'blue');
grunt.task.run(['logColor']);

Will result in the color blue being logged twice. 将导致蓝色被记录两次。

After some fiddling, I came up with a grunt task that allows dynamically specifying a different option/config for each sub-task to be run. 经过一番摆弄后,我想出了一个笨拙的任务,允许为每个要运行的子任务动态指定不同的选项/配置。 I've published the task as grunt-galvanize . 我已经将这项任务发布为grunt-galvanize Here's how it works: 以下是它的工作原理:

var galvanizeConfig = [
  {options: {color: 'red'}, configs: {}},
  {options: {color: 'blue'}, configs: {}}
];
grunt.option('galvanizeConfig', galvanizeConfig);
grunt.task.run(['galvanize:log']);

This will log red then blue , as desired by running the log task with each of the options/configs specified in galvanizeConfig . 通过使用galvanizeConfig指定的每个选项/配置运行日志任务,这将根据需要记录红色然后蓝色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM