rschmukler / gulp-insert

string mutation library for gulp
MIT License
61 stars 6 forks source link

how do I insert text to the end of the stream? #7

Open yairEO opened 9 years ago

yairEO commented 9 years ago

I don't want to insert text into every file. I only want to add it to the end of the whole stream, so if I have many files in my gulp.src() it would be added to the end. is that possible?

iansinnott commented 9 years ago

+1. I was using insert.prepend but was getting a similar issue. I wanted to prepend a shebang to the file, but gulp-insert was editing the end of the file regardless. If there is a way to strictly just prepend (or append as @yairEO would like) a string of text then please let us know. Thanks.

jamiter commented 9 years ago

You could use gulp-concat to concat your files first and then use insert.append to add your text at the end.

concat  = require 'gulp-concat'
insert  = require 'gulp-insert'

gulp.src ['.ext']
  .pipe doYourStuff()
  .pipe concat 'together.ext'
  .pipe insert.append '# something at the end'
  .pipe gulp.dest '/target'
yairEO commented 9 years ago

I want to insert something only of some condition has been met. What in your opinion would be the best way? I can't insert an if statement in the middle of functions chaining. I guess I could save the stream into a variable, test the condition, and only in the end save the stream to disk using dest function

richarddewit commented 9 years ago

I'd suggest gulp-if

var gulpif = require('gulp-if');
var insert = require('gulp-insert');

var condition = true; // TODO: add business logic

gulp.task('task', function() {
  gulp.src('./src/*.js')
    .pipe(doYourStuff())
    .pipe(gulpif(condition, insert.append('# something at the end')))
    .pipe(gulp.dest('./dist/'));
});