簡體   English   中英

Grunt-ssh和grunt-watch在更改的文件上通過sftp

[英]Grunt-ssh and grunt-watch to sftp on changed files

我試圖讓grunt-ssh sftp與grunt-watch一起使用,只發送修改后的文件。 我設法使它正常工作但存在有線問題

我的咕unt配置

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:{'./': []}
        },

    },
});

監視事件功能以修改配置

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();
});

這是咕unt的輸出

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

在此輸出中,您可以看到文件的控制台日志每次都是正確的,但它似乎並不總是上載到正確的文件,我無法弄清原因。 任何幫助將非常感激

我通過在sftp任務之前添加等待來解決此問題,以確保配置有時間更新

grunt.registerTask('wait', 'Wait for a set amount of time.', function() {

    delay = 1;

    var d = delay ? delay + ' second' + (delay === '1' ? '' : 's') : 'forever';
    grunt.log.write('Waiting ' + d + '...');
    // Make this task asynchronous. Grunt will not continue processing
    // subsequent tasks until done() is called.
    var done = this.async();
    // If a delay was specified, call done() after that many seconds.
    if (delay) { setTimeout(done, delay * 1000); }

});

更改了監視任務:

watch: {
        upload: {
            files: ['./*.*', './resources/**/*.*', './protected/**/*.*'],
            tasks: ['wait', 'sftp:upload'],
            options: {
                spawn: false,
            },
        }
    },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM