Grunt-ssh和grunt-watch to sftp on changed files

时间:2017-06-16 10:55:49

标签: javascript node.js gruntjs grunt-contrib-watch grunt-ssh

我试图让grunt-ssh sftp与grunt-watch一起使用,只发送修改过的文件。我设法让这个工作但有线问题

my grunt config

grunt.initConfig({
    watch: {
        upload: {
            files: ['*.*'],
            tasks: ['sftp'],
            options: {
                spawn: false,
            },
        }
    },
    sftp:{
        upload:{
            options: {
                host: '192.168.10.10',
                username: 'blah',
                password: 'blah',
                path:'/home/vagrant/Sites/test/',
                showProgress: true,
            },
            files:{'./': []}
        },

    },
});

在watch事件上运行以修改配置

var changedFiles = {'./': []};
var onChange = grunt.util._.debounce(function() {
    grunt.config('sftp.upload.files', changedFiles);
    changedFiles = {'./': []};
    console.log(grunt.config('sftp.upload.files'))
}, 275);
grunt.event.on('watch', function(action, filepath) {
    changedFiles['./'].push(filepath);
    onChange();
});

这是grunt的输出

Running "watch" task
Waiting...
{ './': [ 'Gruntfile.js' ] }
>> File "Gruntfile.js" changed.

Running "sftp:upload" (sftp) task
Gruntfile.js [====================] 100% of 6KB
Created 1 directories, copied 1 files

Running "watch" task
Completed in 0.617s at Fri Jun 16 2017 11:47:56 GMT+0100 (BST) - 
Waiting...

Reloading watch config...

Running "watch" task
Waiting...
{ './': [ 'affiliate_with_log.php', 'Gruntfile.js' ] }
>> File "affiliate_with_log.php" changed.
>> File "Gruntfile.js" changed.

Running "sftp:upload" (sftp) task
affiliate_with_log.php [====================] 100% of 10KB
Gruntfile.js [====================] 100% of 6KB
Created 1 directories, copied 2 files

Running "watch" task
Completed in 0.546s at Fri Jun 16 2017 11:48:08 GMT+0100 (BST) - 
Waiting...
>> File "affiliate_with_log.php" changed.
>> File "img.php" changed.

Running "sftp:upload" (sftp) task
{ './': [ 'affiliate_with_log.php', 'img.php' ] }
{ './': [ 'affiliate_with_log.php', 'img.php' ] }
{ './': [ 'affiliate_with_log.php', 'img.php' ] }
affiliate_with_log.php [====================] 100% of 10KB
Gruntfile.js [====================] 100% of 6KB
Created 1 directories, copied 2 files

Running "watch" task 
Completed in 0.538s at Fri Jun 16 2017 11:48:19 GMT+0100 (BST) - 
Waiting...
>> File "img.php" changed.

Running "sftp:upload" (sftp) task
{ './': [ 'img.php' ] }
{ './': [ 'img.php' ] }
{ './': [ 'img.php' ] }
affiliate_with_log.php [====================] 100% of 10KB
img.php [====================] 100% of 877B
Created 1 directories, copied 2 files

在这个输出中,您可以看到文件的控制台日志每次都是正确的,但似乎并不总是上传到正确的文件,我无法解决原因。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我通过在sftp任务之前添加等待来确保配置有时间更新

来解决这个问题
watch: {
        upload: {
            files: ['./*.*', './resources/**/*.*', './protected/**/*.*'],
            tasks: ['wait', 'sftp:upload'],
            options: {
                spawn: false,
            },
        }
    },

更改了监视任务:

var http = require('https');
var qs = require('querystring');
var request = require('request');

var appid = appid
var Oauth = Oauth
var Authorization = 'Basic' + ' '+ new Buffer(appid + ':' + 
Oauth).toString('base64');
var url = "https://example.com"

var options = {
host: url,
headers:{'Authorization' : Authorization,
"Content-Type": "application/json"}
};

request.post({
url:url,
headers:{'Authorization' : Authorization,
"Content-Type": "application/json"}
}, 
function(request, response){
console.log(response)
var body = JSON.parse(response["body"])
var token = body["access_token"]
//console.log(token)
return token

});