Skip to content

Commit

Permalink
Ignore folders
Browse files Browse the repository at this point in the history
Check if the current file is a folder, and if so do not try to add headers. Trying to add the headers on folders gives errors...
  • Loading branch information
laureysruben authored Jun 13, 2016
1 parent 237442b commit 6d3506e
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var through = require('through2');
var gutil = require('gulp-util');
var stream = require('stream');
var path = require('path');
var fs = require('fs');

/**
* gulp-header plugin
Expand All @@ -19,7 +20,7 @@ var path = require('path');
module.exports = function (headerText, data) {
headerText = headerText || '';

function TransformStream (file, enc, cb) {
function TransformStream(file, enc, cb) {
var filename;
var concat;

Expand All @@ -31,25 +32,27 @@ module.exports = function (headerText, data) {
filename = '';
}

var template = data === false ? headerText : gutil.template(headerText, extend({file : file, filename: filename}, data));
var template = data === false ? headerText : gutil.template(headerText, extend({ file: file, filename: filename }, data));
concat = new Concat(true, filename);

if (file.isBuffer()) {
concat.add(null, new Buffer(template));
}
if (!fs.lstatSync(file.path).isDirectory()) {

if (file.isStream()) {
var stream = through();
stream.write(new Buffer(template));
stream.on('error', this.emit.bind(this, 'error'));
file.contents = file.contents.pipe(stream);
this.push(file);
return cb();
}
if (file.isBuffer()) {
concat.add(null, new Buffer(template));
}

// add sourcemap
concat.add(file.relative, file.contents, file.sourceMap);
if (file.isStream()) {
var stream = through();
stream.write(new Buffer(template));
stream.on('error', this.emit.bind(this, 'error'));
file.contents = file.contents.pipe(stream);
this.push(file);
return cb();
}

// add sourcemap
concat.add(file.relative, file.contents, file.sourceMap);
}
// make sure streaming content is preserved
if (file.contents && !isStream(file.contents)) {
file.contents = concat.content;
Expand All @@ -74,6 +77,6 @@ module.exports = function (headerText, data) {
* is stream?
*/

function isStream (obj) {
function isStream(obj) {
return obj instanceof stream.Stream;
}

1 comment on commit 6d3506e

@dlarsson
Copy link

@dlarsson dlarsson commented on 6d3506e Jun 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to build a 3rd party module, using gulp-header, I'm running into trouble with this change. The gulpfile tries to add a header to a file generated by a pipe, and thus the file doesn't exist in the filesystem yet, and this causes fs.lstatSync to throw an exception, silently aborting the stream.

nvm, just noticed there's a pull request dealing with this already

Please sign in to comment.