使用gulp来打印/记录文件内容

3
我正在处理一个gulp任务,需要调试一些文件并在控制台中读取它们。例如,我想要在控制台中输出它们的内容。 无论是通过流还是简单任务都可以。我试图打印vinyl对象的内容...但是没有任何好的结果。
gulp.task('task', function(){
    console.log( gulp.src('path/to/file.txt').content.toString() );
});

或者通过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节点模块相同的结果...来完成它。
fs.readFile('path/to/file.txt', {encoding: 'utf-8', flag: 'rs'}, function(e, data) {
  if (e) return console.log(e);
  console.log(data);
});

有没有办法打印文件的内容?

https://dev59.com/Hmox5IYBdhLWcg3wf0bl - Gopinath Shiva
@gopinathshiva 这正是我想要做的事情:[ - davesnx
1个回答

1
这正是我之前回答中提到的问题,gulp/node 的异步性会在文件创建之前尝试读取该文件...
用 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);
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接