node.js - 如何使用 process.stdin 作为 gulp 任务的起点?

标签 node.js gulp vinyl

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

通过阅读 process.stdin 是一个 ReadableStreamvinyl 似乎可以包装 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

最佳答案

因此,我通过处理 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();
    });
};

关于node.js - 如何使用 process.stdin 作为 gulp 任务的起点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34814017/

相关文章:

node.js - @Types/Sequelize 错误 TS1086 : An accessor cannot be declared in ambient context

node.js - 在部署到 lambda 时包括本地依赖项

haskell - Vinyl:使用需要所有字段共享约束的函数进行 rtraverse

node.js - Gulp vinyl ftp - 如何使用清洁功能?

node.js - 为什么服务器在第二次请求时崩溃?

node.js - 如何使 node_modules 成为全局的而不是项目内的文件夹

Node.js:全局声明node_modules,还是根据需要在本地声明?

asp.net-core - 在 VS Code 中构建之前运行 Gulp 任务

node.js - 如果从 gulp 启动,则使用 pm2 启动应用程序

javascript - Gulp 任务完成且没有错误,但没有文件保存到目标