index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* jshint node: true */
  2. 'use strict';
  3. var through = require('through2');
  4. var gutil = require('gulp-util');
  5. var extend = require('object-assign');
  6. var headerPlugin = function(headerText, data) {
  7. headerText = headerText || '';
  8. var stream = through.obj(function(file, enc, cb) {
  9. var template = gutil.template(headerText, extend({file : file}, data));
  10. if (file.isBuffer()) {
  11. file.contents = Buffer.concat([
  12. new Buffer(template),
  13. file.contents
  14. ]);
  15. }
  16. if (file.isStream()) {
  17. var stream = through();
  18. stream.write(new Buffer(template));
  19. stream.on('error', this.emit.bind(this, 'error'));
  20. file.contents = file.contents.pipe(stream);
  21. }
  22. // make sure the file goes through the next gulp plugin
  23. this.push(file);
  24. // tell the stream engine that we are done with this file
  25. cb();
  26. });
  27. // returning the file stream
  28. return stream;
  29. };
  30. module.exports = headerPlugin;