Comparing version 0.4.0 to 0.5.0
{ | ||
"spaceUnits": 4, | ||
"useTabChar": false, | ||
"language": { | ||
"javascript": { | ||
"linting.prefer": [ "JSHint", "JSCS" ], | ||
"linting.prefer": [ | ||
"JSHint", | ||
"JSCS" | ||
], | ||
"linting.usePreferredOnly": true | ||
}, | ||
"markdown": { | ||
"wordWrap": true | ||
} | ||
} | ||
} |
57
index.js
/* jshint node: true */ | ||
var path = require('path'); | ||
var es = require('event-stream'); | ||
var readFiles = require('read-vinyl-file-stream'); | ||
var encodings = ['utf8', 'utf-8', 'buffer']; | ||
var defaultEnc = 'utf8'; | ||
function castData(data, enc) { | ||
var isBuffer = Buffer.isBuffer(data); | ||
if (enc === 'buffer') { | ||
return isBuffer ? data : (new Buffer(data)); | ||
} else { | ||
return isBuffer ? data.toString(enc) : data; | ||
} | ||
} | ||
function each(iterator, enc, context) { | ||
@@ -28,42 +19,10 @@ if (enc && typeof enc === 'string') { | ||
} | ||
var doEach = function(file, callback) { | ||
// continue if the file is null | ||
if (file.isNull()) { | ||
return callback(null, file); | ||
} | ||
var filepath = file.path || file.history[0]; | ||
var name = path.basename(filepath); | ||
var iteratorFunc = context ? iterator.bind(context) : iterator; | ||
var content; | ||
var cb = function iteratorCallback(err, newContent) { | ||
if (file.isStream()) { | ||
file.contents = es.readArray([newContent]); | ||
callback(null, file); | ||
} else { | ||
file.contents = new Buffer(newContent); | ||
callback(null, file); | ||
} | ||
}; | ||
if (file.isStream()) { | ||
file.contents.pipe(es.wait(function(err, data) { | ||
data = castData(data, enc); | ||
iteratorFunc(data, file, cb); | ||
})); | ||
} else if (file.isBuffer()) { | ||
content = castData(file.contents, enc); | ||
iteratorFunc(content, file, cb); | ||
} else { | ||
// not sure what else it could be, but just deal with it | ||
callback(null, file); | ||
} | ||
}; | ||
return es.map(doEach); | ||
var iteratorFunc = context ? iterator.bind(context) : iterator; | ||
return readFiles(function (content, file, stream, cb) { | ||
iteratorFunc(content, file, cb); | ||
}, enc); | ||
} | ||
module.exports = each; |
{ | ||
"name": "gulp-each", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A for each that provides the raw file content.", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"event-stream": "^3.3.1" | ||
"read-vinyl-file-stream": "^2.0.2" | ||
}, | ||
@@ -19,4 +19,6 @@ "devDependencies": { | ||
"codeclimate-test-reporter": "^0.1.0", | ||
"event-stream": "^3.3.1", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^2.3.0", | ||
"mocha": "^3.1.0", | ||
"through2": "^2.0.3", | ||
"vinyl": "^0.5.1" | ||
@@ -23,0 +25,0 @@ }, |
# gulp-each | ||
[![Build][1]][2] [![Test Coverage][3]][4] [![Code Climate][5]][6] [![Downloads][7]][8] [![Version][9]][8] [![ISC License][10]][11] [![Analytics][12]][13] | ||
[![Build][1]][2] | ||
[![Test Coverage][3]][4] | ||
[![Code Climate][5]][6] | ||
[![Downloads][7]][8] | ||
[![Version][9]][8] | ||
[![Dependency Status][10]][11] | ||
[![ISC License][12]][13] | ||
[![Analytics][14]][15] | ||
[1]: https://img.shields.io/travis/catdad/gulp-each/master.svg?style=flat-square | ||
[1]: https://travis-ci.org/catdad/gulp-each.svg?branch=master | ||
[2]: https://travis-ci.org/catdad/gulp-each | ||
[3]: https://img.shields.io/codeclimate/coverage/github/catdad/gulp-each.svg?style=flat-square | ||
[3]: https://codeclimate.com/github/catdad/gulp-each/badges/coverage.svg | ||
[4]: https://codeclimate.com/github/catdad/gulp-each/coverage | ||
[5]: https://img.shields.io/codeclimate/github/catdad/gulp-each.svg?style=flat-square | ||
[5]: https://codeclimate.com/github/catdad/gulp-each/badges/gpa.svg | ||
[6]: https://codeclimate.com/github/catdad/gulp-each | ||
[7]: https://img.shields.io/npm/dm/gulp-each.svg?style=flat-square | ||
[7]: https://img.shields.io/npm/dm/gulp-each.svg | ||
[8]: https://www.npmjs.com/package/gulp-each | ||
[9]: https://img.shields.io/npm/v/gulp-each.svg?style=flat-square | ||
[9]: https://img.shields.io/npm/v/gulp-each.svg | ||
[10]: https://img.shields.io/npm/l/gulp-each.svg?style=flat-square | ||
[11]: http://opensource.org/licenses/ISC | ||
[10]: https://david-dm.org/catdad/gulp-each.svg | ||
[11]: https://david-dm.org/catdad/gulp-each | ||
[12]: https://ga-beacon.appspot.com/UA-17159207-7/gulp-each/readme?flat | ||
[13]: https://github.com/igrigorik/ga-beacon | ||
[12]: https://img.shields.io/npm/l/gulp-each.svg | ||
[13]: http://opensource.org/licenses/ISC | ||
[14]: https://ga-beacon.appspot.com/UA-17159207-7/gulp-each/readme | ||
[15]: https://github.com/igrigorik/ga-beacon | ||
A for-each for Gulp that exposes the actual content of the file. | ||
@@ -42,3 +52,3 @@ | ||
gulp.task('mytask', function() { | ||
gulp.src('*.js') | ||
return gulp.src('*.js') | ||
.pipe(each(function(content, file, callback) { | ||
@@ -63,3 +73,3 @@ // content is a string containing the code | ||
gulp.task('mytask', function() { | ||
gulp.src('*.png') | ||
return gulp.src('*.png') | ||
.pipe(each(function(content, file, callback) { | ||
@@ -66,0 +76,0 @@ // content is a buffer containing the image |
135
test.js
@@ -10,40 +10,68 @@ /* jshint node: true, expr: true */ | ||
var es = require('event-stream'); | ||
var through = require('through2'); | ||
var each = require('./'); | ||
var fakeData = 'llama'; | ||
function fileBuffer(opts) { | ||
opts = opts || {}; | ||
return new File({ | ||
contents: new Buffer(opts.content || 'fake file'), | ||
path: opts.path || Math.random().toString(36).slice(2) + '.txt', | ||
base: __dirname | ||
}); | ||
} | ||
function inputStream(dataArr) { | ||
var input = through.obj(); | ||
setImmediate(function () { | ||
dataArr.forEach(function (file) { | ||
input.push(file); | ||
}); | ||
input.end(); | ||
}); | ||
return input; | ||
} | ||
describe('Buffers', function() { | ||
var each, input; | ||
var input; | ||
beforeEach(function() { | ||
each = require('./index.js'); | ||
input = function() { | ||
return new File({ | ||
contents: new Buffer(fakeData), //es.readArray([fakeData]), | ||
path: 'file.ext', | ||
base: __dirname | ||
return new fileBuffer({ | ||
content: fakeData, | ||
path: 'file.ext' | ||
}); | ||
}; | ||
}); | ||
it('gets called once per file', function(done) { | ||
var count = 0; | ||
var source = each(function(content, file, cb) { | ||
var source = through.obj(); | ||
source.pipe(each(function(content, file, cb) { | ||
count++; | ||
cb(null, content); | ||
}); | ||
source.on('end', done); | ||
})) | ||
.on('end', done) | ||
.on('error', done) | ||
.on('data', function () {}); | ||
source.write(input()); | ||
expect(count).to.equal(1); | ||
source.write(input()); | ||
expect(count).to.equal(2); | ||
source.end(); | ||
}); | ||
it('takes a buffer as a source', function(done) { | ||
@@ -53,10 +81,10 @@ var source = each(function(content, file, cb) { | ||
cb(null, content); | ||
done(); | ||
}); | ||
source.write(input()); | ||
source.end(); | ||
}); | ||
it('can output a buffer in the iterator function', function(done) { | ||
@@ -67,6 +95,6 @@ var source = each(function(content, file, cb) { | ||
cb(null, content); | ||
done(); | ||
}, 'buffer'); | ||
source.write(input()); | ||
@@ -78,6 +106,5 @@ source.end(); | ||
describe('Streams', function() { | ||
var each, input; | ||
var input; | ||
beforeEach(function() { | ||
each = require('./index.js'); | ||
input = function() { | ||
@@ -91,3 +118,3 @@ return new File({ | ||
}); | ||
it('takes a stream as a source', function(done) { | ||
@@ -97,10 +124,10 @@ var source = each(function(content, file, cb) { | ||
cb(null, content); | ||
done(); | ||
}); | ||
source.write(input()); | ||
source.end(); | ||
}); | ||
it('can output a buffer in the iterator function', function(done) { | ||
@@ -111,6 +138,6 @@ var source = each(function(content, file, cb) { | ||
cb(null, content); | ||
done(); | ||
}, 'buffer'); | ||
source.write(input()); | ||
@@ -122,15 +149,13 @@ source.end(); | ||
describe('general', function() { | ||
var each, input, obj = {}; | ||
var input, obj = {}; | ||
beforeEach(function() { | ||
each = require('./index.js'); | ||
input = function() { | ||
return new File({ | ||
contents: new Buffer(fakeData), //es.readArray([fakeData]), | ||
path: 'file.ext', | ||
base: __dirname | ||
return new fileBuffer({ | ||
content: fakeData, | ||
path: 'file.ext' | ||
}); | ||
}; | ||
}); | ||
it('can be called with a `this` arguments', function(done) { | ||
@@ -140,10 +165,34 @@ var source = each(function(content, file, cb) { | ||
expect(this).to.equal(obj); | ||
cb(null, content); | ||
done(); | ||
}, obj); | ||
source.write(input()); | ||
source.end(); | ||
}); | ||
// this test simulates a gulp task, to make sure this | ||
// module is compatible in a pipeline | ||
it('writes vinyl files as the output', function (done) { | ||
var files = [fileBuffer(), fileBuffer(), fileBuffer()]; | ||
var count = 0; | ||
inputStream(files) | ||
.pipe(each(function(content, file, cb) { | ||
cb(null, content); | ||
})) | ||
.on('data', function onFile(file) { | ||
expect(file).to.be.instanceOf(File); | ||
expect(file).to.have.property('contents'); | ||
count += 1; | ||
}) | ||
.on('error', done) | ||
.on('end', function () { | ||
expect(count).to.equal(files.length); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11437
9
182
84
7
+ Addedcore-util-is@1.0.3(transitive)
+ Addedduplexer2@0.1.4(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addednode-stream@1.7.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedread-vinyl-file-stream@2.0.3(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsplit2@2.2.0(transitive)
+ Addedstream-combiner2@1.1.1(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedevent-stream@^3.3.1
- Removedduplexer@0.1.2(transitive)
- Removedevent-stream@3.3.5(transitive)
- Removedfrom@0.1.7(transitive)
- Removedmap-stream@0.0.7(transitive)
- Removedpause-stream@0.0.11(transitive)
- Removedsplit@1.0.1(transitive)
- Removedstream-combiner@0.2.2(transitive)
- Removedthrough@2.3.8(transitive)