Skip to content

Instantly share code, notes, and snippets.

@imcodetolive
Last active August 29, 2015 14:13
Show Gist options
  • Save imcodetolive/0a16a89180c5500a4615 to your computer and use it in GitHub Desktop.
Save imcodetolive/0a16a89180c5500a4615 to your computer and use it in GitHub Desktop.

Revisions

  1. @webpro webpro revised this gist May 6, 2014. 1 changed file with 15 additions and 4 deletions.
    19 changes: 15 additions & 4 deletions modify.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,32 @@
    function modify(modifier) {
    function modify(modifiers) {

    return through2.obj(function(file, encoding, done) {

    var stream = this;

    function applyModifiers(content) {
    (typeof modifiers === 'function' ? [modifiers] : modifiers).forEach(function(modifier) {
    content = modifier(content, file);
    });
    return content;
    }

    function write(data) {
    file.contents = new Buffer(data);
    stream.push(file);
    done();
    }

    if (file.isBuffer()) {
    write(modifier(String(file.contents)));
    write(applyModifiers(String(file.contents)));
    } else if (file.isStream()) {
    var buffer = '';
    file.contents.on('data', function(chunk) {
    buffer += chunk;
    });
    file.contents.on('end', function() {
    write(modifier(String(buffer)));
    write(applyModifiers(String(buffer)));
    });
    }
    });
    }
    }
  2. @webpro webpro created this gist May 5, 2014.
    21 changes: 21 additions & 0 deletions modify.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function modify(modifier) {
    return through2.obj(function(file, encoding, done) {
    var stream = this;
    function write(data) {
    file.contents = new Buffer(data);
    stream.push(file);
    done();
    }
    if (file.isBuffer()) {
    write(modifier(String(file.contents)));
    } else if (file.isStream()) {
    var buffer = '';
    file.contents.on('data', function(chunk) {
    buffer += chunk;
    });
    file.contents.on('end', function() {
    write(modifier(String(buffer)));
    });
    }
    });
    }
    12 changes: 12 additions & 0 deletions task.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    gulp.src('app.js')
    .pipe(modify(version))
    .pipe(modify(swapStuff))
    .pipe(gulp.dest('build/'));

    function version(data) {
    return data.replace(/__VERSION__/, '0.0.1');
    }

    function swapStuff(data) {
    return data.replace(/(\w+)\s(\w+)/, '$2, $1');
    }