有关
我能够使用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', } } },
但是当我运行它时,这会上传所有内容.有数千个文件.每次修改文件时,我都没有时间等待他们逐个上传.
一旦我更改了文件,如何设置手表才能上传我已更改的文件?
(好奇的是,服务器是本地网络上的虚拟机.它运行在不同的操作系统上,设置与我的本地机器的设置更相似.如果我能正常工作,上传应该是快速的)
你需要的是grunt-newer,一个特别设计的任务,根据刚改变的文件更新任何任务的配置,然后运行它.示例配置可能如下所示:
watch: { all: { files: ['**','!{node_modules,artifacts,sql,logs}/**'], tasks: ['newer:sftp:dev'] } }
你可以做到这一点使用手表事件的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'); };
请注意"spawn:false"选项,以及在事件处理程序中设置配置所需的方式.
注意2:此代码一次上传一个文件,在同一个链接中有一个更健壮的方法.