Node (Gulp) process.stdout.write to file

I try to have gulp take care of my unit tests for me and output my test coverage to a .lcov file.

This is what I have so far:

 gulp.task('test', function () { var test = fs.createWriteStream('./test.lcov', {flags: 'a'}); return gulp.src('./assets/js/test/test.js', {read: false}) .pipe(mocha({reporter: 'mocha-lcov-reporter'})) .pipe(test); }); 

The mocha-lcov-reporter code can be found here: https://github.com/StevenLooman/mocha-lcov-reporter/blob/master/lib/lcov.js

It outputs the results through process.stdout.write()

But when I connect this to WriteStream , I have the following error:

 TypeError: Invalid non-string/buffer chunk at validChunk (_stream_writable.js:152:14) at WriteStream.Writable.write (_stream_writable.js:181:12) at Stream.ondata (stream.js:51:26) at Stream.emit (events.js:95:17) at drain (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:36:16) at Stream.stream.queue.stream.push (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:45:5) at Stream.stream (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/index.js:27:8) at Stream.stream.write (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:26:11) at write (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24) at flow (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7) 
+5
source share
2 answers

It seems that gulp -mocha is not fully configured as a true end-to-end stream, in fact it looks like it just passes the source to the Mocha instance and allows Mocha to do this.

The first thing that comes to my mind is to simply redirect to bash ...

 $ gulp test | grep -Ev "^\[[0-9:]{0,8}\]" > ./test.lcov 

Of course, it is assumed that all output related to gulp starts at [00:00:00] (with 00 being the current system time). If this is not the case, you can get gulp output at the top and bottom of the file.

If you are looking for a more universal answer (read: using nodejs), you can do the correspondence process.stdout.write . This is probably possible :( most, but it will work. The trick is that you cannot rewrite process.stdout as another thread, because it is written as a receiver inside. However, you can rewrite the stdout.write function. I just did it for of the project I'm working on, so I can view gulp logs from other developers if they have problems with the build system.

I decided to go with a not-so-asynchronous solution, because unlike most others in nodejs, stdout and stderr are both blocking threads and do not act like the asynchronous code you're used to. Using this method, your task will look something like this:

 gulp.task('test', function () { // clear out old coverage file fs.writeFileSync('./test.lcov', ''); // if you still want to see output in the console // you need a copy of the original write function var ogWrite = process.stdout.write; process.stdout.write = function( chunk ){ fs.appendFile( './test.lcov', chunk ); // this will write the output to the console ogWrite.apply( this, arguments ); }; return gulp.src('./assets/js/test/test.js', {read: false}) .pipe(mocha({reporter: 'mocha-lcov-reporter'})); }); 
+3
source

I needed to write everything in my gulp process to a file and it ended up working fine for me:

 var fs = require('fs'); var proc = require('process'); var origstdout = process.stdout.write, origstderr = process.stderr.write, outfile = 'node_output.log', errfile = 'node_error.log'; if (fs.exists(outfile)) { fs.unlink(outfile); } if (fs.exists(errfile)) { fs.unlink(errfile); } process.stdout.write = function( chunk ){ fs.appendFile(outfile, chunk.replace(/\x1b\[[0-9;]*m/g, '')); origstdout.apply(this, arguments); }; process.stderr.write = function( chunk ){ fs.appendFile(errfile, chunk.replace(/\x1b\[[0-9;]*m/g, '')); origstderr.apply(this, arguments); }; 
0
source

Source: https://habr.com/ru/post/1204793/


All Articles