Skip to content

Commit

Permalink
Merge 9cba0da into 99e0494
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate-Wilkins committed Jan 23, 2016
2 parents 99e0494 + 9cba0da commit 2b2a120
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ module.exports = function gulpWrap(opts, data, options) {
promise = ES6Promise.resolve(opts);
}

data = data || {};
options = options || {};

if (!options.engine) {
options.engine = 'lodash';
}

return through.obj(function gulpWrapTransform(file, enc, cb) {
function compile(contents, done) {
if (typeof data === 'function') {
data = data.call(null, file);
}

if (typeof options === 'function') {
options = options.call(null, file);
}

data = data || {};
options = options || {};

if (!options.engine) {
options.engine = 'lodash';
}

// attempt to parse the file contents for JSON or YAML files
if (options.parse !== false) {
var ext = path.extname(file.path).toLowerCase();
Expand Down
39 changes: 38 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ describe('gulp-wrap', function() {
.end(new File({contents: new Buffer('Hello')}));
});

it('should allow for dynamic options', function(done) {
var srcFile = new File({contents: new Buffer('Hello')});
srcFile.dataProp = 'data';

wrap(
'BEFORE <%= data.contents %> <%= data.someVar %> AFTER',
{someVar: 'someVal'},
function(file) {
return {variable: file.dataProp};
}
)
.on('error', done)
.on('data', function(file) {
assert(file.isBuffer());
assert.equal(String(file.contents), 'BEFORE Hello someVal AFTER');
done();
})
.end(srcFile);
});

it('should allow file props in the template data', function(done) {
var srcFile = new File({contents: new Buffer('Hello')});
srcFile.someProp = 'someValue';
Expand Down Expand Up @@ -113,6 +133,24 @@ describe('gulp-wrap', function() {
.end(srcFile);
});

it('should allow for dynamic data', function(done) {
var srcFile = new File({contents: new Buffer('Hello')});
srcFile.someProp = 'bar';

wrap('<%= contents %> - <%= file.someProp %>', function(file) {
return {
file: {someProp: 'foo-' + file.someProp}
};
})
.on('error', done)
.on('data', function(file) {
assert(file.isBuffer());
assert.equal(String(file.contents), 'Hello - foo-bar');
done();
})
.end(srcFile);
});

it('should not pollute file data across multiple streams', function(done) {
var srcFile1 = new File({contents: new Buffer('1')});
srcFile1.foo = 'one';
Expand Down Expand Up @@ -206,5 +244,4 @@ describe('gulp-wrap', function() {
contents: new Buffer('name: foo')
}));
});

});

0 comments on commit 2b2a120

Please sign in to comment.