-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
104 lines (86 loc) · 2.59 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
var through = require('through2');
var ngDep = require('ng-dependencies');
var toposort = require('toposort');
var PluginError = require('plugin-error');
var PLUGIN_NAME = 'gulp-angular-filesort';
var ANGULAR_MODULE = 'ng';
module.exports = function angularFilesort() {
var files = [];
var ngModules = {};
var toSort = [];
function transformFunction(file, encoding, next) {
// Fail on empty files
if (file.isNull()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'File: "' + file.relative + '" without content. You have to read it with gulp.src(..)'));
return;
}
// Streams not supported
if (file.isStream()) {
/* jshint validthis:true */
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
next();
return;
}
var deps;
try {
deps = ngDep(file.contents);
} catch (err) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Error in parsing: "' + file.relative + '", ' + err.message));
return;
}
if (deps.modules) {
// Store references to each file with a declaration:
Object.keys(deps.modules).forEach(function(name) {
ngModules[name] = file;
});
}
if (deps.dependencies) {
// Add each file with dependencies to the array to sort:
deps.dependencies.forEach(function(dep) {
if (isDependecyUsedInAnyDeclaration(dep, deps)) {
return;
}
if (dep === ANGULAR_MODULE) {
return;
}
toSort.push([file, dep]);
});
}
files.push(file);
next();
}
function flushFunction(next) {
// Convert all module names to actual files with declarations:
for (var i = 0; i < toSort.length; i++) {
var moduleName = toSort[i][1];
var declarationFile = ngModules[moduleName];
if (declarationFile) {
toSort[i][1] = declarationFile;
} else {
// Depending on module outside stream (possibly a 3rd party one),
// don't care when sorting:
toSort.splice(i--, 1);
}
}
// Sort `files` with `toSort` as dependency tree:
toposort.array(files, toSort)
.reverse()
.forEach(function(file) {
this.push(file);
}.bind(this));
next();
}
return through.obj(transformFunction, flushFunction);
};
function isDependecyUsedInAnyDeclaration(dependency, ngDeps) {
if (!ngDeps.modules) {
return false;
}
if (dependency in ngDeps.modules) {
return true;
}
return Object.keys(ngDeps.modules).some(function(module) {
return ngDeps.modules[module].indexOf(dependency) > -1;
});
}