简体   繁体   English

Grunt - 在运行任务之前动态设置配置

[英]Grunt - Dynamically Set Config Before Running a Task

So here is my hypothetical config object for a hypothetical fooTask that does something (not relevant to question) to a bunch of JS files 所以这是我假设的fooTask的假设配置对象,它对一堆JS文件做了一些事情(与问题无关)

grunt.initConfig({
  fooTask: {
    app1: 'app1/*.js',
    app2: 'app2/*.js',
    app3: 'app3/*.js'
  }
});

As you can see, with this approach, I have to run fooTask 3 times with each app specified as a target: 正如您所看到的,使用这种方法,我必须在指定为目标的每个应用程序中运行fooTask 3次:

grunt fooTask:app1
grunt fooTask:app2
grunt fooTask:app3

Needless to say this does not scale as either the number of apps increase or the number of such foo tasks increase as one has to C&P the same code over and over for each app. 毋庸置疑,随着应用程序数量的增加或此类foo任务的数量增加,这不会扩展,因为必须为每个应用程序反复使用C&P相同的代码。

So ideally what I would like to define is just one target with the name of the app passed in as a config variable 理想情况下,我想要定义的只是一个目标,其中app作为配置变量传入

grunt.initConfig({
  fooTask: {
    dist: '<%=appName%>/*.js'
  }
});

I would then like to call fooTask 3 times, one for each app, with the right app set as appName 然后我想调用fooTask 3次,每个应用程序一个,并将正确的应用程序设置为appName

var apps = ['app1', 'app2', 'app3'];
apps.forEach(function(app) {
  var currAppName = app;

  // Run fooTask but how do I specify the new currAppName config?
  grunt.task.run('fooTask');
});

As from code above, I know I can run my fooTask using grunt.task.run but how do I set the appName config for my task? 从上面的代码中,我知道我可以使用grunt.task.run运行我的grunt.task.run但是如何为我的任务设置appName配置?

Note that this question is similar to this other one that also does not have the right answer yet - Pass Grunt config options from task.run 请注意,这个问题类似于另一个也没有正确答案的问题 - 从task.run传递Grunt配置选项

Thanks a lot. 非常感谢。

EDIT 2: 编辑2:

So nevermind the garbage below the first edit, leaving as example of what doesn't work. 所以永远不要在第一次编辑下面的垃圾,留下不起作用的例子。 In my case it was really important to be able to set the value within a task at run-time so I settled on the file system. 在我的情况下,能够在运行时在任务中设置值非常重要,因此我确定了文件系统。 Perhaps it suits your needs. 也许它适合您的需求。

grunt.initConfig({
  someTask: {
    someKey: fs.readFileSync('file.txt', { encoding: 'utf8' })
  }
});

of course you can do the readFile outside of the task if you need a bunch of different app names. 当然,如果你需要一堆不同的应用程序名称,你可以在任务之外执行readFile。


EDIT: Hmmm. 编辑:嗯。 I swear I had this working when I wrote this...but now it is not. 我发誓,当我写这篇文章时,我有这个工作......但现在却没有。 Grunt just sees the extra arguments as additional unfound tasks. Grunt只是将额外的参数视为额外的不完整任务。


I was trying to figure this out myself, finally a "duh" just moment happened - why not parse process.argv before grunt.initConfig ? 我试图自己解决这个问题,最后一个“呃”刚刚发生 - 为什么不在grunt.initConfig之前解析process.argv

module.exports = function(grunt) {

  var sourcefile = process.argv[2] || 'default.js'; // <- this

  grunt.initConfig({
    uglify: {
      main: {
        src: sourcefile, // <- voila :)
        dest: sourcefile.substring(0, sourcefile.length-3) + '.min.js'
      }
    }
  });

  grunt.loadNpmTasks('uglify');

  grunt.registerTask('default', ['uglify']);
};

and use from command line: 并从命令行使用:

grunt mykillerscript.js

I didn't even try to use grunt.option for the same reason that all the examples only showed directing which task is run, but I wouldn't be surprised if there is a more "grunt" way to do this. 我甚至没有尝试使用grunt.option,因为所有示例都只显示了指导哪个任务运行,但如果有更“笨拙”的方法,我也不会感到惊讶。

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

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