-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (51 loc) · 1.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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.emit('data', files[module.defined]);
_.each(module.contents, function (path) {
stream.emit('data', 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) {
if (chunk.isNull()) {
// do nothing if no contents
}
if (chunk.isBuffer()) {
angularModulesFactory.processFile(chunk.contents.toString(), chunk.path);
}
if (chunk.isStream()) {
return this.emit('error',
new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
files[chunk.path] = chunk;
}, function () {
var topology = angularModulesFactory.getAngularModules();
var that = this;
if (modules && modules.length) {
_.each(modules, function (module) {
_.each(topology.resolve(module, true), function (module) {
addToStream(that, module);
});
});
} else {
_.each(topology.modules, function (module) {
addToStream(that, module);
});
}
this.emit('end');
});
}
// exporting the plugin main function
module.exports = gulpAngularDependency;