Skip to content

Commit

Permalink
Add a restore function
Browse files Browse the repository at this point in the history
  • Loading branch information
IxDay committed Sep 5, 2014
1 parent 05045cb commit bb39f06
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
43 changes: 33 additions & 10 deletions index.js
Expand Up @@ -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
}
Expand All @@ -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) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 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": {
Expand Down Expand Up @@ -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"
Expand Down
45 changes: 40 additions & 5 deletions test/main.js
Expand Up @@ -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) {
Expand All @@ -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();
})())
}))
});
});

0 comments on commit bb39f06

Please sign in to comment.