onkami
onkami

Reputation: 9411

how to modify config files using gulp

I use gulp to configure complex local setup and need to auto-edit files.

The scenario is:

I need this to amend system configuration files and compile scenarios.

What would be the best way to do it in gulp?

Upvotes: 3

Views: 3523

Answers (2)

vitalymak
vitalymak

Reputation: 47

Or you can use vinyl-map:

const map = require('vinyl-map')
const gulp = require('gulp')

const modify = map((contents, filename) => {
    contents = contents.toString()

    // modify contents somehow

    return contents
  })

gulp.task('modify', () =>
  gulp.src(['./index.js'])
    .pipe(modify)
    .pipe(gulp.dest('./dist'))
})

Upvotes: 0

Caio Cunha
Caio Cunha

Reputation: 23394

Gulp is plain javascript. So what I would do if I were you is to create a plugin to pipe to the original config file.

Gulp streams emit Vinyl files. So all you really got to do is to create a "pipe factory" that transforms the objects.

It would look something like this (using EventStream):

var es = require('event-stream');

// you could receive params in here if you're using the same
// plugin in different occasions.
function fixConfigFile() {
  return es.map(function(file, cb) {
    var fileContent = file.contents.toString();

    // determine if certain file contains certain lines...
    // if line is not found, insert the line.
    // optionally, delete some lines found in the file.

    // update the vinyl file
    file.contents = new Buffer(fileContent);

    // send the updated file down the pipe
    cb(null, file);
  });
}

gulp.task('fix-config', function() {
  return gulp.src('path/to/original/*.config')
    .pipe(fixConfigFile())
    .pipe(gulp.dest('path/to/fixed/configs');
});

Upvotes: 3

Related Questions