gulp print/log 一个文件的内容

gulp print/log the content of a file

我正在处理一个 gulp 任务,它想要调试一些文件,例如,我想在控制台中读取它。

无论是按流还是按简单任务,都无所谓。 我尝试打印乙烯基对象的内容......但没有任何好结果

gulp.task('task', function(){
    console.log( gulp.src('path/to/file.txt').content.toString() );
});

through node

function sendTo(res) {   
    return through(
        function write(data) {    // this will be called once for each file
            res.write(data.contents);
        },
        function end() {    // this will be called when there are no more files
            res.end()
        }
    );
}

gulp.task('task', function(res){
    gulp.src('path/to/file.txt')
        .pipe(sendTo(res));
});

或者尝试用 fs node module 来做同样的结果...

fs.readFile('path/to/file.txt', {encoding: 'utf-8', flag: 'rs'}, function(e, data) {
  if (e) return console.log(e);
  console.log(data);
});

知道打印文件的内容吗?

这正是我的另一个答案的问题,gulp/node 的异步尝试在创建文件之前读取文件...

简单的形式是节点(是的,我是对的:P)

fs.readFile('path/to/file.txt', {encoding: 'utf-8', flag: 'rs'}, function(e, data) {
  if (e) return console.log(e);
  console.log(data);
});