Gruntjs 如何动态构建监视任务&x27;谁的目标?
使用Gruntjs 如何动态构建监视任务&x27;谁的目标?,gruntjs,Gruntjs,使用gurunt syncall,我想将项目根目录中的每个目录(以及在将来创建的新目录)动态同步到另一个目录,除了“node\u modules”目录 使用grunt watchall,我想查看那些dir,当dir中有一些更改时,我想自动运行相应的同步任务 以下是我的项目根目录的结构: ├── Gruntfile.js ├── addon1 │ └── a.toc ├── adon3 │ └── 3.toc ├── node_modules │ ├── grunt │ ├── g
gurunt syncall
,我想将项目根目录中的每个目录(以及在将来创建的新目录)动态同步到另一个目录,除了“node\u modules”目录
使用grunt watchall
,我想查看那些dir,当dir中有一些更改时,我想自动运行相应的同步任务
以下是我的项目根目录的结构:
├── Gruntfile.js
├── addon1
│ └── a.toc
├── adon3
│ └── 3.toc
├── node_modules
│ ├── grunt
│ ├── grunt-contrib-watch
│ └── grunt-sync
└── package.json
grunt syncall
命令正常,结果如下:
➜ testsync grunt syncall
Running "config" task
Running "sync:addon1" (sync) task
Running "sync:adon3" (sync) task
Done, without errors.
但是grunt watchall
不正常你能告诉我为什么watchall
任务不起作用以及如何修复它吗?
我启动命令,将文件'3.toc'更改并保存在dir'adon3'中,然后grunt说:
➜ testsync grunt watchall
Running "config" task
Running "watch" task
Waiting...
>> File "adon3/3.toc" changed.
Running "sync:adon3" (sync) task
Verifying property sync.adon3 exists in config...ERROR
>> Unable to process task.
Warning: Required config property "sync.adon3" missing. Use --force to continue.
Aborted due to warnings.
这是我的Grunfile.js:
module.exports = function(grunt) {
grunt.initConfig({});
var destdir = "/Users/morrxy/project/testdest/";
// dynamic config sync and watch's targets for every dir except for node_modules
grunt.registerTask('config', 'config sync and watch', function() {
grunt.file.expand({filter: 'isDirectory'},
['*', '!node_modules']).forEach(function(dir) {
// config sync's targets
var sync = grunt.config.get('sync') || {};
sync[dir] = {
files: [{
cwd: dir,
src: '**',
dest: destdir + dir
}]
};
grunt.config.set('sync', sync);
// config watch's target
var watch = grunt.config.get('watch') || {};
watch[dir] = {
files: dir + '/**/*',
// is next line has problem?
// running 'grunt watchall'
// when I change and save the file '3.toc' in dir 'adon3', terminal say:
// >> File "adon3/3.toc" changed.
// Running "sync:adon3" (sync) task
// Verifying property sync.adon3 exists in config...ERROR
// >> Unable to process task.
// Warning: Required config property "sync.adon3" missing.
// but why 'grunt syncall' can work?
tasks: 'sync:' + dir
};
grunt.config.set('watch', watch);
});
});
grunt.loadNpmTasks('grunt-sync');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('syncall', ['config', 'sync']);
grunt.registerTask('watchall', ['config', 'watch']);
};
最后,我解决了这个问题。我为这个问题添加了一个github回购协议。如果您想要测试,您可以克隆它并在本地运行它 之所以
watchall
可以监视文件更改,但无法运行相应的同步任务,是因为当watch find files更改时,每个同步的配置都会停止运行,因此该同步目标的配置将消失,我们没有将运行的配置保存在任何位置。因此,为了解决这个问题,我们可以在同步任务之前将config任务添加到watch任务中,如下面的tasks:['config\u sync','sync:'+dir]
在新的Gruntfile.js中,我使用配置同步任务将配置任务拆分为两个任务,一个用于配置监视,一个用于配置同步。这是新的Gruntfile.js
module.exports = function(grunt) {
var destdir = "/media/data/projects/dynasync_dest/";
grunt.loadNpmTasks('grunt-sync');
grunt.loadNpmTasks('grunt-contrib-watch');
// dynamic config sync's targets for every dir except for node_modules
grunt.registerTask('config_sync', 'dynamically config sync', function() {
grunt.file.expand({filter: 'isDirectory'},
['*', '!node_modules']).forEach(function(dir) {
// config this dir's sync target
var sync = grunt.config.get('sync') || {};
sync[dir] = {
files: [{
cwd: dir,
src: '**/*',
dest: destdir + dir
}]
};
grunt.config.set('sync', sync);
});
});
// dynamic config watch's targets for every dir except for node_modules
grunt.registerTask('config_watch', 'dynamically config watch', function() {
grunt.file.expand({filter: 'isDirectory'},
['*', '!node_modules']).forEach(function(dir) {
// config this dir's watch target
var watch = grunt.config.get('watch') || {};
watch[dir] = {
files: dir + '/**/*',
// this line solve the problem
// when find file change, first dynamically config sync and then sync the dir
tasks: ['config_sync', 'sync:' + dir]
};
grunt.config.set('watch', watch);
});
});
grunt.registerTask('syncall', ['config_sync', 'sync']);
grunt.registerTask('watchall', ['config_watch', 'watch']);
grunt.registerTask('default', ['watchall']);
};
这一次,当watch find文件发生更改时,它可以运行相应的同步任务。像这样
grunt watchall
Running "config_watch" task
Running "watch" task
Waiting...
>> File "adon3/3.toc" changed.
Running "config_sync" task
Running "sync:adon3" (sync) task
Done, without errors.
Completed in 0.888s at Thu Apr 10 2014 14:01:26 GMT+0800 (CST) - Waiting...