【问题标题】:grunt-contrib-watch + grunt-rsyncgrunt-contrib-watch + grunt-rsync
【发布时间】:2013-12-06 23:31:02
【问题描述】:

我正在尝试使用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' 手动执行此操作

【问题讨论】:

    标签: gruntjs rsync grunt-contrib-watch


    【解决方案1】:

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

    目前执行上述操作的最佳方法是使用手表事件配置您的 rsync 任务,但使用手表的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

    【讨论】:

    • 我认为是产卵的东西把我搞砸了。较新版本的手表有一个maximum call stack 问题。我降级回 0.4.x 但后来又停止工作了。不得不更改以将选项更改为nospawn:true
    • 即便如此,Grunt 也会在每次更改后重新启动对每个文件的监视。我有 19K 个文件,所以这需要几分钟,这显然太慢了。我需要在大约半秒内完成整个过程。
    • 该问题与您的问题不同。你为什么看 19K 文件? grunt-contrib-watch(也不是 AFAIK,当前在 node.js 中的任何其他手表)将处理 19K 文件,更不用说在 500 毫秒以下。
    • 不在 Ubuntu 上。我将它置于详细模式,我可以看到它在每个文件上创建一个监视,一次一个。我找到了另一个库grunt-este-watch,它只在目录上创建监视,并且不会在每次文件更改后重新创建它们。在 1.5 秒内启动(约 1775 个目录),然后快速启动。它有时会在我的 IDE 创建的 ___jb_bak___ 文件上触发一个事件,但这些很容易过滤掉。此外,它会为您提供每次更改的回调,因此我们不需要 grunt-newer
    • 很高兴为您工作。我希望我也能放弃我的测试用例 :)
    【解决方案2】:

    即使使用Kyle's solutiongrunt-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;
        }
    },
    

    【讨论】:

    • 谢谢你。 grunt-contrib-watch 在 Windows 上所做的所有零星挂起正在慢慢变得疯狂。 grunt-este-watch 更快、更干净、更可靠。
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 2017-06-12
    • 2023-03-12
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    相关资源
    最近更新 更多