2016-08-18 103 views
0

我一直在使用gulp-rsync通過gulp任務將靜態資產部署到我的服務器。我使用增量選項來確定文件是否需要更新。這工作得很好:)Gulp RSync - 將進度保存到日誌文件

我需要保存在一個文件或任何顯示在控制檯的進展.. 因爲我需要對CloudFlare的緩存中的清除單個文件:/

看起來像(在控制檯):

[20:49:52] Starting 'rsync'... 
[20:49:53] gulp-rsync: Starting rsync to my-ssh-hostname:/example/public-html/assets/... 
[20:49:55] gulp-rsync: sending incremental file list 
[20:49:57] gulp-rsync: favicon.ico 
[20:49:57] gulp-rsync:   1150 100% 439.45kB/s 0:00:00 (xfer#1, to-check=12/13) 
[20:49:57] gulp-rsync: css/main.css 
[20:49:57] gulp-rsync:   2712 100% 101.86kB/s 0:00:00 (xfer#2, to-check=11/13) 
[20:49:57] gulp-rsync: css/style.css 
[20:49:57] gulp-rsync:   1445 100% 54.27kB/s 0:00:00 (xfer#3, to-check=9/13) 
[20:49:57] gulp-rsync: js/app.js 
[20:49:57] gulp-rsync:  31878 100% 1.09MB/s 0:00:00 (xfer#7, to-check=3/13) 
[20:49:57] gulp-rsync: scripts.js 
[20:50:01] gulp-rsync:  76988 100% 2.53MB/s 0:00:00 (xfer#9, to-check=1/13) 
[20:50:01] gulp-rsync: sent 2401 bytes received 2820 bytes 10442.00 bytes/sec 
[20:50:02] gulp-rsync: total size is 10106 speedup is 4.37 
[20:50:02] gulp-rsync: Finished 'rsync' after 3.38 s 

我需要在日誌保存和提取文件:

favicon.ico, 
css/main.css, 
css/style.css, 
js/app.js, 
scripts.js 

- 我原來的 「gulpfile.js」:

var 
    gulp = require('gulp'), 
    gutil = require('gulp-util'), 
    rsync = require('gulp-rsync'), 
    logCapture = require('gulp-log-capture'); 

var config = { 
    hostname : 'my-ssh-hostname', 
    destination : '/example/public-html/assets/', 
    progress: true, 
    incremental: true, 
    relative: true, 
    emptyDirectories: true, 
    recursive: true, 
    clean: true, 
    exclude: ['._', '.DS_Store' , 'thumbs.db', 'desktop.ini'], 
    chmod: '775', 
}; 

gulp.task('rsync', function(){ 
    return gulp.src('./my-local-dir/' + '**/*') 
    .pipe(rsync(config)) 
}); 

- 我發現了 「咕嘟咕嘟日誌捕獲插件」 - gulp-log-capture

什麼是使用正確的方式? :/

gulp.task('rsync', function(){ 
    return gulp.src('./my-local-dir/' + '**/*') 
    .pipe(logCapture.start(process.stdout, 'write')) 
    .pipe(rsync(config)) 
    .pipe(logCapture.stop('txt')) 
    .pipe(gulp.dest('./dest')); 
}); 

任何sugestions將apreciated :)

回答

0

不能使用gulp-log-capture這是因爲的方式,gulp-rsync作品。

gulp-rsync在它調用rsync之前等待它收集所有文件名。否則,它將不得不爲每個單獨的文件調用rsync,這會嚴重降低性能。

所以到rsync被調用的時候logCapture已經停止捕獲日誌輸出。

你要聽的'end'事件,並通過自己的功能(這是什麼gulp-log-capture一樣)代process.stdout.write()捕捉自己的日誌輸出:

gulp.task('rsync', function(){ 
    var logOutput = ""; 
    var stdoutWrite; 
    return gulp.src('./my-local-dir/' + '**/*') 
    .on('end', function() { 
     stdoutWrite = process.stdout.write; 
     process.stdout.write = function(output) { logOutput += output; }; 
    }) 
    .pipe(rsync(config)) 
    .on('end', function() { 
     process.stdout.write = stdoutWrite; 
     process.stdout.write(logOutput); 
     fs.writeFileSync('./dest/logOutput.txt', new Buffer(logOutput)); 
    }); 
}); 

此存儲由rsync產生的進度日誌在logOutput變量中,並將其寫入文件./dest/logOutput.txt

logOutput中提取文件名現在只是提出適當的正則表達式的問題。我會把那部分留給你。如果這給你帶來麻煩,最好問問以上的人。

+0

工程就像一個魅力!謝謝,這有助於澄清我的想法! :) – WhoAmI

相關問題