cwonrails
2/13/2014 - 4:23 AM

A simple GulpJS plugin for copying resources into a specific folder (and not retaining their previous path)

A simple GulpJS plugin for copying resources into a specific folder (and not retaining their previous path)

var flatCopy = require('./gulp-flat-copy');

// ... 

gulp.task('copy', function(){
  
  // ...
  
  gulp.src(['**/*.js.map'])
    .pipe(flatCopy())
    .pipe(gulp.dest('public/scripts'));
    
  // ... 
  
});    

// ... 
var path = require('path');
var through = require('through');
var File = require('gulp-util').File;

module.exports = function(){
    var files = [];

    function onStream(file){
        files.push(file);
    }

    function onEnd(){
        for(var f in files){
            var file = files[f]
              , split = file.path.split("/")
              , name = split[split.length-1]
              , joinedFile = new File({
                    path: name,
                    contents: new Buffer(file.contents)
                })
              ;
            this.emit('data', joinedFile);
        }

        this.emit('end');
    }

    return through(onStream, onEnd);
}