From 05045cb6f4cf78d8e40c29446155c1f29655bae7 Mon Sep 17 00:00:00 2001 From: Maxime Vidori Date: Thu, 4 Sep 2014 13:34:20 +0200 Subject: [PATCH] Change through2 to event-stream --- index.js | 26 +++++++++++++------------- package.json | 7 ++++--- test/main.js | 38 ++++++++++---------------------------- 3 files changed, 27 insertions(+), 44 deletions(-) diff --git a/index.js b/index.js index e12c417..9e8f147 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,21 @@ -var through = require('through2'); +var es = require('event-stream'); var _ = require('lodash'); +var gutil = require('gulp-util'); +var PluginError = gutil.PluginError; var angularModulesFactory = new (require('angular-dependency/lib')).AngularModulesFactory(); +// consts +const PLUGIN_NAME = 'gulp-angular-dependency'; + // plugin level function (dealing with files) function gulpAngularDependency (modules) { function addToStream (stream, module) { - stream.push(files[module.defined]); + stream.emit('data', files[module.defined]); _.each(module.contents, function (path) { - stream.push(files[path]); + stream.emit('data', files[path]); }); } @@ -19,27 +24,22 @@ function gulpAngularDependency (modules) { // creating a stream through which each file will pass // returning the file stream - return through.obj(function (chunk, enc, cb) { + return es.through(function (chunk) { if (chunk.isNull()) { // do nothing if no contents } if (chunk.isBuffer()) { angularModulesFactory.processFile(chunk.contents.toString(), chunk.path); - cb(); } if (chunk.isStream()) { - chunk.contents = chunk.contents.pipe(through.obj(function (content, enc, done) { - angularModulesFactory.processFile(content.toString(), chunk.path); - done(); - }, function () { - cb(); - })); + return this.emit('error', + new PluginError(PLUGIN_NAME, 'Streaming not supported')); } files[chunk.path] = chunk; - }, function (cb) { + }, function () { var topology = angularModulesFactory.getAngularModules(); var that = this; if (modules && modules.length) { @@ -53,7 +53,7 @@ function gulpAngularDependency (modules) { addToStream(that, module); }); } - cb(); + this.emit('end'); }); } diff --git a/package.json b/package.json index c264f4d..3e378eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-angular-dependency", - "version": "0.1.2", + "version": "0.1.3", "description": "A gulp plugin which will retrieve files needed by angular modules through your filesystem", "main": "index.js", "scripts": { @@ -31,7 +31,8 @@ }, "dependencies": { "angular-dependency": "^0.1.4", - "lodash": "^2.4.1", - "through2": "^0.6.1" + "event-stream": "^3.1.7", + "gulp-util": "^3.0.1", + "lodash": "^2.4.1" } } diff --git a/test/main.js b/test/main.js index ee7943f..ad7ba9e 100644 --- a/test/main.js +++ b/test/main.js @@ -1,9 +1,10 @@ var assert = require('assert'); var vfs = require('vinyl-fs'); -var through = require('through2'); +var es = require('event-stream'); var angularDependency = require('../'); var path = require('path'); - +var gutil = require('gulp-util'); +var PluginError = gutil.PluginError; describe('gulp-angular-dependency', function () { var files; @@ -16,25 +17,8 @@ describe('gulp-angular-dependency', function () { 'dependencies', function (done) { vfs.src('test/test_case/test_case_1/*') .pipe(angularDependency('module1')) - .pipe(through.obj(function (chunk, enc, cb) { - files.push(path.relative(__dirname, chunk.path)); - cb(); - }, function () { - assert.deepEqual(files, [ - 'test_case/test_case_1/file_0_2.js', - 'test_case/test_case_1/file_0_1.js', - 'test_case/test_case_1/file_0_3.js', - 'test_case/test_case_1/file_0_5.js']); - done(); - })); - }); - - it('should support stream as well', function (done) { - vfs.src('test/test_case/test_case_1/*', { buffer: false }) - .pipe(angularDependency('module1')) - .pipe(through.obj(function (chunk, enc, cb) { + .pipe(es.through(function (chunk) { files.push(path.relative(__dirname, chunk.path)); - cb(); }, function () { assert.deepEqual(files, [ 'test_case/test_case_1/file_0_2.js', @@ -48,9 +32,8 @@ describe('gulp-angular-dependency', function () { it('should be possible to require multiple modules', function (done) { vfs.src('test/test_case/test_case_2/*') .pipe(angularDependency(['module1', 'module2'])) - .pipe(through.obj(function (chunk, enc, cb) { + .pipe(es.through(function (chunk) { files.push(path.relative(__dirname, chunk.path)); - cb(); }, function () { assert.deepEqual(files, [ 'test_case/test_case_2/file_0_1.js', @@ -62,16 +45,15 @@ describe('gulp-angular-dependency', function () { it('should pass all files containing angular code if no module is defined', function (done) { - var checking = through.obj(function (chunk, enc, cb) { + var checking = es.through(function (chunk) { files.push(path.relative(__dirname, chunk.path)); - this.push(chunk); - cb(); - }, function (cb) { + this.emit('data', chunk); + }, function () { assert.deepEqual(files, [ 'test_case/test_case_3/file_0_1.js', 'test_case/test_case_3/file_0_3.js', 'test_case/test_case_3/file_0_2.js']); - cb(); + this.emit('end'); }); vfs.src('test/test_case/test_case_3/*') @@ -81,7 +63,7 @@ describe('gulp-angular-dependency', function () { .pipe(checking) .pipe((function () { done(); - return through(); + return es.through(); })()) }); }); \ No newline at end of file