Skip to content

Commit

Permalink
Change through2 to event-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
IxDay committed Sep 4, 2014
1 parent 9ad800a commit 05045cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 44 deletions.
26 changes: 13 additions & 13 deletions 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]);
});
}

Expand All @@ -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) {
Expand All @@ -53,7 +53,7 @@ function gulpAngularDependency (modules) {
addToStream(that, module);
});
}
cb();
this.emit('end');
});
}

Expand Down
7 changes: 4 additions & 3 deletions 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": {
Expand Down Expand Up @@ -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"
}
}
38 changes: 10 additions & 28 deletions 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;
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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/*')
Expand All @@ -81,7 +63,7 @@ describe('gulp-angular-dependency', function () {
.pipe(checking)
.pipe((function () {
done();
return through();
return es.through();
})())
});
});

0 comments on commit 05045cb

Please sign in to comment.