process.stdin如何用作gulp任务的起点?

时间:2016-01-15 15:05:50

标签: node.js gulp vinyl

我使用gulp使用gulp-sass插件将SCSS转换为CSS代码。这一切都运行正常,但我也想使用gulp从Unix管道接收输入(SCSS代码)(即读取process.stdin)并使用它并将输出流式传输到process.stdout

process.stdin周围阅读是ReadableStream,而vinyl似乎可以包裹stdin,然后在gulp任务中使用,例如。

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

1 个答案:

答案 0 :(得分: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();
    });
};