gpt4 book ai didi

gruntjs - grunt-contrib-watch + grunt-rsync

转载 作者:行者123 更新时间:2023-12-04 02:38:22 31 4
gpt4 key购买 nike

我正在尝试使用 grunt-contrib-watchgrunt-rsync将任何文件更改上传到我的开发服务器。这是我的设置:

var path = require('path');

module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

...

watch: {
upload: {
files: ['*'],
},
...
},

rsync: {
options: {
args: ['--verbose'],
exclude: ['.*','node_modules','sql','artifacts','session','logs','cache'],
recursive: true,
syncDestIgnoreExcl: true
},
dev: {
options: {
src: './',
dest: '/usr/local/project',
host: 'dev3',
}
}
},
});

grunt.event.on('watch', function(action, filepath, target) {
if(target!=='upload') return;
grunt.config('rsync.dev.options.src', filepath);
grunt.task.run('rsync:dev');
});

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-rsync');
...
};

rsync 任务本身运行良好;它上传我的整个项目的速度相对较快,但速度不足以在每次文件编辑时运行。

我正在尝试使用 grunt.event.on 在再次运行任务之前修改 src 以便它只上传一个文件。但是,grunt.task.run('rsync:dev') 似乎什么也没做。没有错误,什么都没有。这是我运行它然后创建一个新文件时的输出:

$ grunt watch:upload --debug --stack
Running "watch:upload" (watch) task
Waiting...OK
>> File "ppp" added.

如果我给它一个无效的任务名称,它就会出错,所以它显然是在寻找任务,我只是不确定它在做什么。该文件永远无法到达我的服务器。

我怎样才能让它只同步我保存的文件?

注意newer似乎也不起作用,所以我尝试通过 grunt.event.on('watch'

手动执行此操作

最佳答案

不建议从 watch 事件中运行任务。看看这里的警告:https://github.com/gruntjs/grunt-contrib-watch#using-the-watch-event

目前执行上述操作的最佳方法是使用 watch 事件配置 rsync 任务,但使用 watch 的 tasks 来运行任务,如下所示:

grunt.initConfig({
watch: {
upload: {
// Dont spawn to retain the process context here
options: { spawn: false },
files: ['*'],
tasks: ['rsync:dev']
},
},
});
grunt.event.on('watch', function(action, filepath, target) {
if (target === 'upload') grunt.config('rsync.dev.options.src', filepath);
});

FWIW,我们正在寻找更好的方法来处理上述用例。这是领先的想法:https://github.com/gruntjs/grunt-contrib-watch/issues/156

关于gruntjs - grunt-contrib-watch + grunt-rsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20435439/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com