How can I use Gulp to add a line of text to a file -


i've been trying figure out how add line of text type of file using gulp.

for instance add:

@import 'plugins' 

to main.sass file.

or add cdn index.html file.

i did try:

gulp.task('inject-plugins', function(){    gulp.src('src/css/main.sass')     .pipe(inject.after('// add imports', '\n@import \'plugins\'\n')); }); 

with no joy. idea how achieve please?

depends on want do.

if want add text beginning or end of file gulp-header , gulp-footer friends:

var header = require('gulp-header'); var footer = require('gulp-footer');  gulp.task('add-text-to-beginning', function() {   return gulp.src('src/css/main.sass')     .pipe(header('@import \'plugins\'\n'))     .pipe(gulp.dest('dist')); });  gulp.task('add-text-to-end', function() {   return gulp.src('src/css/main.sass')     .pipe(footer('@import \'plugins\''))     .pipe(gulp.dest('dist')); }); 

if have kind of "anchor" text in file can use gulp-replace:

var replace = require('gulp-replace');  gulp.task('replace-text', function() {   var anchor = '// add imports';   return gulp.src('src/css/main.sass')     .pipe(replace(anchor, anchor + '\n@import \'plugins\'\n'))     .pipe(gulp.dest('dist')); }); 

finally there's swiss army knife of vinyl file manipulation: map-stream. gives direct access file contents , allows kind of string manipulation can think of in javascript:

var map = require('map-stream');  gulp.task('change-text', function() {   return gulp.src('src/css/main.sass')     .pipe(map(function(file, cb) {       var filecontents = file.contents.tostring();       // --- string manipulation here ---       filecontents = filecontents.replace(/foo/, 'bar');       filecontents = 'first line\n' + filecontents;       // ---------------------------------------       file.contents = new buffer(filecontents);       cb(null, file);     }))     .pipe(gulp.dest('dist')); }); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -