How to replace a stream using Gulp?

I am trying to switch from Grunt to Gulp and I have a problem:

I read two streams from two files

var fileStream = gulp.src(file); var injectionStream = gulp.src(injection) .pipe(replace('#class-name#', argv.cname)); 

If my console argument "-remove" is missing, I have no problem concatenating these threads

 .pipe(concat('animation.styl')) .pipe(gulp.dest('./dist')) 

However, when "-remove" is true, I want to remove the injection, in other words, subtract the injection stream from fileStream.

I tried:

 var es = require('event-stream'); es.replace() var replace = require('gulp-replace'); 

It works with strings, but I cannot succeed in streams read from files. Can someone give me a little hint?

And maybe this is the wrong tool for the generation task, and I should stay with Grunt and / or other tools like yo, etc.

Thank you for your time!

+6
source share
2 answers

Good job :) Looks like gulp-replace might be easier for people coming here from Google.

 var replace = require('gulp-replace'); gulp.task('templates', function(){ gulp.src(['file.txt']) .pipe(replace(/foo(.{3})/g, '$1foo')) .pipe(gulp.dest('build/file.txt')); }); 
+9
source

Finally I found a solution!

 fs.readFile(injectionFile, 'utf8', function (err, injStr) { injStr = injStr.replace(/svv/g, cname); fileStream .pipe(gulpif(rm!=true,injectString.append(injStr),replace(injStr,''))) .pipe(concat(initialFile)) .pipe(gulp.dest(animation)) 

...

It took a while, but I'm HAPPY.

thanks

+2
source

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


All Articles