Grunt watch:仅上传已更改的文件

mpe*_*pen 9 ssh gruntjs grunt-contrib-watch

有关

我能够使用grunt-ssh为我的开发服务器设置一个Grunt任务到SFTP文件:

sftp: {
    dev: {
        files: {
            './': ['**','!{node_modules,artifacts,sql,logs}/**'],
        },
        options: {
            path: '/path/to/project',
            privateKey: grunt.file.read(process.env.HOME+'/.ssh/id_rsa'),
            host: '111.111.111.111',
            port: 22,
            username: 'marksthebest',
        }
    }
},
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,这会上传所有内容.有数千个文件.每次修改文件时,我都没有时间等待他们逐个上传.

一旦我更改了文件,如何设置手表才能上传我已更改的文件?

(好奇的是,服务器是本地网络上的虚拟机.它运行在不同的操作系统上,设置与我的本地机器的设置更相似.如果我能正常工作,上传应该是快速的)

Ben*_*Ben 7

你需要的是grunt-newer,一个特别设计的任务,根据刚改变的文件更新任何任务的配置,然后运行它.示例配置可能如下所示:

watch: {
  all: {
    files: ['**','!{node_modules,artifacts,sql,logs}/**'],
    tasks: ['newer:sftp:dev']
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,我可以看到问题.你试过rsync吗?它只上传了改变的文件部分,与传统的FTP相比,它的速度非常快.还有一个grunt包装器:https://github.com/jedrichards/grunt-rsync (3认同)

小智 5

你可以做到这一点使用手表事件grunt-contrib-watch.您基本上需要处理监视事件,修改sftp文件配置以仅包含已更改的文件,然后让grunt运行sftp任务.

像这样的东西:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        secret: grunt.file.readJSON('secret.json'),
        watch: {
            test: {
                files: 'files/**/*',
                tasks: 'sftp',
                options: {
                    spawn: false
                }
            }
        },
        sftp: {
          test: {
            files: {
              "./": "files/**/*"
            },
            options: {
              path: '/path/on/the/server/',
              srcBasePath: 'files/',
              host: 'hostname.com',
              username: '<%= secret.username %>',
              password: '<%= secret.password %>',
              showProgress: true
            }
          }
        }
    }); // end grunt.initConfig

    // on watch events configure sftp.test.files to only run on changed file
    grunt.event.on('watch', function(action, filepath) {
        grunt.config('sftp.test.files', {"./": filepath});
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-ssh');
};
Run Code Online (Sandbox Code Playgroud)

请注意"spawn:false"选项,以及在事件处理程序中设置配置所需的方式.

注意2:此代码一次上传一个文件,在同一个链接中有一个更健壮的方法.


Jos*_*rín 3

您可以使用 Grunt 来实现这一点:

  • grunt-contrib-手表

  • grunt-rsync

首先,我正在使用 Docker 容器。我还在 Docker 容器中添加了一个公共 SSH 密钥。因此,我仅将使用此 Grunt 任务在本地环境中更改的文件上传到“远程”容器中:

'use strict';

module.exports = function(grunt) {

    grunt.initConfig({

        rsync: {
            options: {
                args: ['-avz', '--verbose', '--delete'],
                exclude: ['.git*', 'cache', 'log'],
                recursive: true
            },
            development: {
                options: {
                    src: './',
                    dest: '/var/www/development',
                    host: 'root@www.localhost.com',
                    port: 2222
                }
            }
        },

        sshexec: {
            development: {
                command: 'chown -R www-data:www-data /var/www/development',
                options: {
                    host: 'www.localhost.com',
                    username: 'root',
                    port: 2222,
                    privateKey: grunt.file.read("/Users/YOUR_USER/.ssh/id_containers_rsa")
                }
            }
        },

        watch: {
            development: {
                files: [
                'node_modules',
                'package.json',
                'Gruntfile.js',
                '.gitignore',
                '.htaccess',
                'README.md',
                'config/*',
                'modules/*',
                'themes/*',
                '!cache/*',
                '!log/*'
                ],
                tasks: ['rsync:development', 'sshexec:development']
            }
        },

    });

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

    grunt.registerTask('default', ['watch:development']);
};
Run Code Online (Sandbox Code Playgroud)

祝你好运,黑客快乐!