2016-01-15 21 views
1

我正在使用gulp將gsmp-sass插件轉換成CSS代碼。這一切都工作正常,但我也想用gulp從Unix管道接收輸入(SCSS代碼)(即讀取process.stdin),並將其消耗並輸出到process.stdoutprocess.stdin如何被用作吞吐任務的起點?

從閱讀周圍process.stdinReadableStreamvinyl似乎是它可以包stdin,然後可以在一飲而盡任務起使用,例如

gulp.task('stdin-sass', function() { 
    process.stdin.setEncoding('utf8'); 
    var file = new File({contents: process.stdin, path: './test.scss'}); 
    file.pipe(convert_sass_to_css()) 
     .pipe(gulp.dest('.')); 
}); 

然而,當我這樣做,我得到一個錯誤:

TypeError: file.isNull is not a function 

這讓我覺得,stdin有些特殊,但對node.js的官方文檔說,它是一個真正的ReadableStream

回答

0

所以我得到這個通過處理process.stdin和寫入工作process.stdout

var buffer = require('vinyl-buffer'); 
var source = require('vinyl-source-stream'); 
var through = require('through2'); 

gulp.task('stdio-sass', function() { 
    process.stdin.setEncoding('utf8'); 
    process.stdin.pipe(source('input.scss')) 
     .pipe(buffer()) 
     .pipe(convert_sass_to_css()) 
     .pipe(stdout_stream()); 
}); 


var stdout_stream = function() { 
    process.stdout.setEncoding('utf8'); 
    return through.obj(function (file, enc, complete) { 
     process.stdout.write(file.contents.toString()); 

     this.push(file); 
     complete(); 
    }); 
};