diff --git a/index.js b/index.js index 264fa4e25..040fb70a5 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,10 @@ Gulp.prototype.src = require('./lib/createInputStream'); Gulp.prototype.dest = require('./lib/createOutputStream'); Gulp.prototype.watch = require('./lib/watchFile'); Gulp.prototype.createGlobStream = require('glob-stream').create; -Gulp.prototype.readFile = require('./lib/readFile'); + +Gulp.prototype.formatFile = require('./lib/formatFile'); +Gulp.prototype.bufferFile = require('./lib/bufferFile'); +Gulp.prototype.streamFile = require('./lib/streamFile'); var gulp = new Gulp(); gulp.Gulp = Gulp; diff --git a/lib/bufferFile.js b/lib/bufferFile.js new file mode 100644 index 000000000..3c3a28837 --- /dev/null +++ b/lib/bufferFile.js @@ -0,0 +1,9 @@ +var fs = require('fs'); + +module.exports = function (file, cb) { + fs.readFile(file.path, function (err, data) { + if (err) return cb(err); + file.contents = data; + cb(null, file); + }); +}; \ No newline at end of file diff --git a/lib/createInputStream.js b/lib/createInputStream.js index 347f5d30a..197a985f1 100644 --- a/lib/createInputStream.js +++ b/lib/createInputStream.js @@ -1,11 +1,32 @@ -var es = require('event-stream'), - gs = require('glob-stream'), - readFile = require('./readFile'); +var es = require('event-stream'); +var gs = require('glob-stream'); +var formatFile = require('./formatFile'); + +var bufferFile = require('./bufferFile'); +var streamFile = require('./streamFile'); module.exports = function(glob, opt) { if (!opt) opt = {}; + if (typeof opt.read !== 'boolean') opt.read = true; + if (typeof opt.buffer !== 'boolean') opt.buffer = true; + + var globStream = gs.create(glob, opt.glob); + var stream = globStream.pipe(es.map(formatFile)); + + // dont read anything - just pass names + if (!opt.read) { + return stream; + } + + // read and pass full contents + if (opt.buffer) { + return stream.pipe(es.map(bufferFile)); + } + + // dont buffer anything - just pass streams + if (!opt.buffer) { + return stream.pipe(es.map(streamFile)); + } - var globStream = gs.create(glob, opt); - var stream = globStream.pipe(es.map(readFile)); return stream; }; diff --git a/lib/createOutputStream.js b/lib/createOutputStream.js index fc580b6b9..316bc598e 100644 --- a/lib/createOutputStream.js +++ b/lib/createOutputStream.js @@ -1,7 +1,7 @@ -var es = require('event-stream'), - fs = require('fs'), - path = require('path'), - mkdirp = require('mkdirp'); +var es = require('event-stream'); +var fs = require('fs'); +var path = require('path'); +var mkdirp = require('mkdirp'); module.exports = function(folder, opt) { if (!opt) opt = {}; diff --git a/lib/formatFile.js b/lib/formatFile.js new file mode 100644 index 000000000..c649db224 --- /dev/null +++ b/lib/formatFile.js @@ -0,0 +1,9 @@ +var realBase = require('gulp-util').realBase; + +module.exports = function (file, cb) { + cb(null, { + shortened: realBase(file.base, file.path), + base: file.base, + path: file.path + }); +}; \ No newline at end of file diff --git a/lib/readFile.js b/lib/readFile.js deleted file mode 100644 index 7c2a1b54d..000000000 --- a/lib/readFile.js +++ /dev/null @@ -1,14 +0,0 @@ -var fs = require('fs'), - realBase = require('gulp-util').realBase; - -module.exports = function (file, cb) { - fs.readFile(file.path, function (err, data) { - if (err) return cb(err); - cb(null, { - shortened: realBase(file.base, file.path), - base: file.base, - path: file.path, - contents: data - }); - }); -}; \ No newline at end of file diff --git a/lib/streamFile.js b/lib/streamFile.js new file mode 100644 index 000000000..f191fa399 --- /dev/null +++ b/lib/streamFile.js @@ -0,0 +1,6 @@ +var fs = require('fs'); + +module.exports = function (file, cb) { + file.contents = fs.createReadStream(file.path); + cb(null, file); +}; \ No newline at end of file diff --git a/lib/watchFile.js b/lib/watchFile.js index e10e959d3..e58f77308 100644 --- a/lib/watchFile.js +++ b/lib/watchFile.js @@ -1,5 +1,5 @@ -var gaze = require('gaze'), - EventEmitter = require('events').EventEmitter; +var gaze = require('gaze'); +var EventEmitter = require('events').EventEmitter; module.exports = function(glob, cb) { var out = new EventEmitter(); diff --git a/package.json b/package.json index e94d48606..a08cbb9ce 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name":"gulp", "description":"The streaming build system", - "version":"1.1.0", + "version":"1.2.0", "homepage":"http://github.com/wearefractal/gulp", "repository":"git://github.com/wearefractal/gulp.git", "author":"Fractal (http://wearefractal.com/)", @@ -18,7 +18,7 @@ "optimist":"0.6.0", "gulp-util":"1.0.0", "gaze":"0.4.2", - "orchestrator": "0.0.5" + "orchestrator": "0.0.6" }, "devDependencies":{ "mocha":"*", diff --git a/test/file-utils.js b/test/file-utils.js index d016d4a2f..47fd54366 100644 --- a/test/file-utils.js +++ b/test/file-utils.js @@ -1,23 +1,73 @@ var gulp = require('../'); var should = require('should'); -var join = require('path').join; +var path = require('path'); +var join = path.join; +var dirname = path.dirname; require('mocha'); -describe('gulp readFile', function() { - describe('readFile()', function() { - it('should return a valid file struct', function(done) { +describe('gulp bufferFile', function() { + describe('bufferFile()', function() { + it('should return a valid file file', function(done) { var fname = join(__dirname, "./fixtures/test.coffee"); - gulp.readFile({path:fname,base:fname}, function(err, struct) { + var base = dirname(fname); + gulp.bufferFile({path:fname,base:base}, function(err, file) { should.not.exist(err); - should.exist(struct); - should.exist(struct.path); - should.exist(struct.contents); - should.exist(struct.base); - struct.path.should.equal(fname); - struct.base.should.equal(fname); - String(struct.contents).should.equal("this is a test"); + should.exist(file); + should.exist(file.path); + should.exist(file.contents); + should.exist(file.base); + file.path.should.equal(fname); + file.base.should.equal(base); + String(file.contents).should.equal("this is a test"); done(); }); }); }); }); + +describe('gulp streamFile', function() { + describe('streamFile()', function() { + it('should return a valid file file', function(done) { + var fname = join(__dirname, "./fixtures/test.coffee"); + var base = dirname(fname); + gulp.streamFile({path:fname,base:base}, function(err, file) { + should.not.exist(err); + should.exist(file); + should.exist(file.path); + should.exist(file.contents); + should.exist(file.base); + file.path.should.equal(fname); + file.base.should.equal(base); + + var buf = ""; + file.contents.on('data', function(d){ + buf += d; + }); + file.contents.on('end', function(){ + buf.should.equal("this is a test"); + done(); + }); + }); + }); + }); +}); + +describe('gulp formatFile', function() { + describe('formatFile()', function() { + it('should return a valid file file', function(done) { + var fname = join(__dirname, "./fixtures/test.coffee"); + var base = dirname(fname); + gulp.formatFile({path:fname,base:base}, function(err, file) { + should.not.exist(err); + should.exist(file); + should.exist(file.shortened); + should.exist(file.path); + should.exist(file.base); + file.path.should.equal(fname); + file.base.should.equal(base); + file.shortened.should.equal("test.coffee"); + done(); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/src.js b/test/src.js index d9b7e1731..80537d5be 100644 --- a/test/src.js +++ b/test/src.js @@ -25,6 +25,37 @@ describe('gulp input stream', function() { done(); }); }); + it('should return a input stream with no contents when read is false', function(done) { + var stream = gulp.src(join(__dirname, "./fixtures/*.coffee"), {read: false}); + stream.on('error', done); + stream.on('data', function(file) { + should.exist(file); + should.exist(file.path); + should.not.exist(file.contents); + join(file.path,'').should.equal(join(__dirname, "./fixtures/test.coffee")); + }); + stream.on('end', function() { + done(); + }); + }); + it('should return a input stream with contents as stream when buffer is false', function(done) { + var stream = gulp.src(join(__dirname, "./fixtures/*.coffee"), {buffer: false}); + stream.on('error', done); + stream.on('data', function(file) { + should.exist(file); + should.exist(file.path); + should.exist(file.contents); + var buf = ""; + file.contents.on('data', function(d){ + buf += d; + }); + file.contents.on('end', function(){ + buf.should.equal("this is a test"); + done(); + }); + join(file.path,'').should.equal(join(__dirname, "./fixtures/test.coffee")); + }); + }); it('should return a input stream from a deep glob', function(done) { var stream = gulp.src(join(__dirname, "./fixtures/**/*.jade")); stream.on('error', done);