3

我正在尝试使用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.

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

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

NB newer似乎也不起作用,所以我试图通过手动执行此操作grunt.event.on('watch'

4

2 回答 2

2

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

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

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

于 2013-12-08T17:46:47.743 回答
2

即使使用凯尔的解决方案grunt-contrib-watch也太慢了(至少在 linux 上)。我找到了另一个库grunt-este-watch,它运行得更快,并且可以让您在每次更改之前更新 rsync 配置,而无需任何黑客攻击。我的设置现在看起来像这样:

esteWatch: {
    options: {
        dirs: [
            '**/',
            '!node_modules/**/',
            '!sql/**/',
            '!artifacts/**/',
            '!session/**/',
            '!logs/**/',
            '!cache/**/',
            '!plugins/ezc/**/',
        ],
        livereload: {
            enabled: true,
            port: 35729,
            extensions: ['css'],
        }
    },
    '*': function(filepath) {
        if(grunt.file.isMatch(['**/*___jb*'],filepath)) return []; // ignore temporary JetBrains files
        var tasks = ['rsync'];
        if(grunt.file.isMatch(grunt.config('uglify.dist.src'),filepath)) tasks.push('uglify');
        if(grunt.file.isMatch(grunt.config('less.dist.src'),filepath)) tasks.push('less');
        grunt.config('rsync.dev.options.src', filepath);
        grunt.config('rsync.dev.options.dest', path.join('/path/to/project',filepath));
        return tasks;
    }
},
于 2013-12-18T16:40:51.410 回答