From bb39f06e9ccde104f772c08d076817ed5a511f28 Mon Sep 17 00:00:00 2001 From: Maxime Vidori Date: Fri, 5 Sep 2014 17:19:36 +0200 Subject: [PATCH] Add a restore function --- index.js | 43 +++++++++++++++++++++++++++++++++---------- package.json | 4 ++-- test/main.js | 45 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 9e8f147..21ad4de 100644 --- a/index.js +++ b/index.js @@ -3,28 +3,35 @@ 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) { + var restoreStream = es.through(); + var angularModulesFactory = + new (require('angular-dependency/lib')).AngularModulesFactory(); + var files = {}; + function addToStream (stream, module) { - stream.emit('data', files[module.defined]); + + if (files[module.defined]) { + stream.emit('data', files[module.defined]); + delete files[module.defined]; + } + _.each(module.contents, function (path) { - stream.emit('data', files[path]); + if (files[path]) { + stream.emit('data', files[path]); + delete files[path]; + } }); } - var files = {}; if (typeof modules === 'string') { modules = [modules]; } - // creating a stream through which each file will pass - // returning the file stream - return es.through(function (chunk) { + var stream = es.through(function (chunk) { if (chunk.isNull()) { // do nothing if no contents } @@ -40,7 +47,7 @@ function gulpAngularDependency (modules) { files[chunk.path] = chunk; }, function () { - var topology = angularModulesFactory.getAngularModules(); + var topology = angularModulesFactory.angularModules(); var that = this; if (modules && modules.length) { _.each(modules, function (module) { @@ -53,8 +60,24 @@ function gulpAngularDependency (modules) { addToStream(that, module); }); } + _.each(files, function (file) { + restoreStream.emit('data', file); + }); + + restoreStream.emit('end'); this.emit('end'); }); + + stream.restore = function (options) { + options = options || {}; + if (options.end) { + return restoreStream; + } + + return restoreStream.pipe(es.through(), { end: false }); + }; + + return stream; } // exporting the plugin main function diff --git a/package.json b/package.json index 3e378eb..e8c8d52 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-angular-dependency", - "version": "0.1.3", + "version": "0.1.4", "description": "A gulp plugin which will retrieve files needed by angular modules through your filesystem", "main": "index.js", "scripts": { @@ -30,7 +30,7 @@ "vinyl-fs": "^0.3.7" }, "dependencies": { - "angular-dependency": "^0.1.4", + "angular-dependency": "^0.1.5", "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 ad7ba9e..75569ac 100644 --- a/test/main.js +++ b/test/main.js @@ -43,6 +43,41 @@ describe('gulp-angular-dependency', function () { })); }); + it('should provide a restore function which will reinject the files ' + + 'filtered out', function (done) { + var angularFilter = angularDependency('module1'); + var restoredFiles = []; + + vfs.src('test/test_case/test_case_3/*') + .pipe(angularFilter) + .pipe(es.through( + function (chunk) { + files.push(path.relative(__dirname, chunk.path)); + 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']); + this.emit('end'); + })) + .pipe(angularFilter.restore()) + .pipe(es.through( + function (chunk) { + restoredFiles.push(path.relative(__dirname, chunk.path)); + this.emit('data', chunk); + }, + function () { + assert.deepEqual(restoredFiles, [ + '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', + 'test_case/test_case_3/file_0_4.js']); + done(); + })); + }); + + it('should pass all files containing angular code if no module is defined', function (done) { var checking = es.through(function (chunk) { @@ -55,15 +90,15 @@ describe('gulp-angular-dependency', function () { 'test_case/test_case_3/file_0_2.js']); this.emit('end'); }); - + var angularFilter = angularDependency(); vfs.src('test/test_case/test_case_3/*') - .pipe(angularDependency()) + .pipe(angularFilter) .pipe(checking) + .pipe(angularFilter.restore()) .pipe(angularDependency([])) .pipe(checking) - .pipe((function () { + .pipe(es.wait(function () { done(); - return es.through(); - })()) + })) }); }); \ No newline at end of file