现在我有了Gruntfile设置来执行一些自动检测魔法,比如解析源文件来解析roder中的一些PHP源文件,以便在运行grunt.initConfig()
之前动态找出我需要知道的文件名和路径。如何在grunt.initConfig()之前执行异步操作?
不幸的是grunt.initConfig()
似乎并不是异步运行,所以我没有办法让我的异步代码在我可以调用之前执行。有没有一个技巧来实现这一点,还是我必须同步重写我的检测程序?在我的回调到达之前有没有简单的方法来阻止执行?
里面的咕噜声任务当然有this.async()
,但是对于initConfig()
不起作用。
这里有一个剥离下来例如:
function findSomeFilesAndPaths(callback) {
// async tasks that detect and parse
// and execute callback(results) when done
}
module.exports = function (grunt) {
var config = {
pkg: grunt.file.readJSON('package.json'),
}
findSomeFilesAndPaths(function (results) {
config.watch = {
coffee: {
files: results.coffeeDir + "**/*.coffee",
tasks: ["coffee"]
// ...
}
};
grunt.initConfig(config);
grunt.loadNpmTasks "grunt-contrib-coffee"
// grunt.loadNpmTasks(...);
});
};
任何好的想法如何完成这件事?
非常感谢!
会发生什么? – 2013-05-14 15:55:14
这不是我上面做的吗?会发生什么是grunt不会等待我的回调,因此在grunt客户端返回之前不会调用grunt.initConfig()等。 – leyyinad 2013-05-14 15:58:12
哦,是的,你做到了,我的错误... – 2013-05-14 20:57:40