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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14863959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:46:13  来源:igfitidea点击:

Programmatically set options for grunt task?

javascriptgruntjs

提问by Nick Heiner

I have a grunt task that looks at options with grunt.option('foo'). If I'm calling this task from grunt.task.run('my-task'), how can I change those arguments?

我有一个繁重的任务,它查看带有grunt.option('foo'). 如果我从 调用此任务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 questionis 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。)

采纳答案by Nick Heiner

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.

全局设置选项而不是仅针对该命令设置选项感觉有点奇怪,但它有效。

回答by jjt

If you can use task-based config options instead of grunt.option, this should work to give you more granular control:

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

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

回答by Alessandro Pezzato

Create a new task which set the option, then call the modified task. This is a real life example with assemble:

创建一个设置选项的新任务,然后调用修改后的任务。这是一个真实的assemble例子:

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

回答by Remi

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

$ grunt build-prod

-OR-

-或者-

$ grunt build-live

$ 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'.

他们都将完成完整的任务“构建”,并分别将一个值传递给assemble选项之一,即生产“真”或“假”。



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}}

在 assemble 中,您可以选择添加一个 {{#if production}}do this on production{{else}}do this not non production{{/if}}

回答by guy mograbi

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 = {};

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

I am using it for my project and it works.

我在我的项目中使用它并且它有效。

回答by nimblengineer

I recently ran up against this same issue: programmatically setting grunt options and running tasks multiple times from within a single parent task. As @Raphael Verger mentions, this is not possible, as grunt.task.rundefers the running of the task until the current task is finished:

我最近遇到了同样的问题:以编程方式设置 grunt 选项并从单个父任务中多次运行任务。正如@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 bluebeing 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. Here's how it works:

经过一番折腾,我想出了一个 grunt 任务,它允许为每个要运行的子任务动态指定不同的选项/配置。我已将任务发布为grunt-galvanize。这是它的工作原理:

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

This will log redthen blue, as desired by running the logtask with each of the options/configs specified in galvanizeConfig.

这将记录,然后蓝色,通过运行如期望的日志与每个指定的选项/ CONFIGS的任务galvanizeConfig