-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
168 lines (135 loc) · 3.58 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
var fs = require('fs'),
path = require('path'),
Vinyl = require('vinyl'),
Log = require('fancy-log'),
AnsiColors = require('ansi-colors'),
merge = require('lodash.merge'),
Stream = require('stream'),
DAG = require('dag'),
cloneRegexp = require('clone-regexp'),
minimatch = require('minimatch');
var PLUGIN_NAME = 'gulp-resolve-dependencies';
function resolveDependencies(config) {
var defaults = {
pattern: /\* @requires [\s-]*(.*\.js)/g,
log: false,
ignoreCircularDependencies: false,
resolvePath: function(match, targetFile) {
return path.join(path.dirname(path.resolve(targetFile.path)), match);
},
exclude: [],
include: []
},
stream,
dag = new DAG(),
fileCache = [],
filesReturned = [],
getFiles = function(targetFile) {
var pattern = cloneRegexp(config.pattern, {
lastIndex: 0
}),
files = [],
content,
match,
filePath,
file,
dependencies;
// Skip if already added to dependencies
if (fileCache.includes(targetFile.path)) {
return false;
} else {
fileCache.push(targetFile.path);
}
content = targetFile.contents.toString('utf8');
while (match = pattern.exec(content)) {
filePath = config.resolvePath(match[1], targetFile);
// Ignore falsy returns
if (!filePath) {
if (config.log) {
Log('[' + AnsiColors.green(PLUGIN_NAME) + '] Ignored', match[1]);
}
continue;
}
// Check include
if (config.include && config.include.length !== 0 && !config.include.some(_ => minimatch(filePath, _))) {
if (config.log) {
Log('[' + AnsiColors.green(PLUGIN_NAME) + '] File not included:', filePath);
}
continue;
}
// Check exclude
if (config.exclude && config.exclude.some(_ => minimatch(filePath, _))) {
if (config.log) {
Log('[' + AnsiColors.green(PLUGIN_NAME) + '] File excluded:', filePath);
}
continue;
}
// Check for circular dependencies
try {
dag.addEdge(targetFile.path, filePath);
} catch (e) {
// Emit error or just continue
if (!config.ignoreCircularDependencies) {
stream.emit('error', new Error(PLUGIN_NAME + ': Circular dependency between "' + targetFile.path + '" and "' + filePath + '"'));
} else {
continue;
}
}
// Check existence
if (!fs.existsSync(filePath)) {
stream.emit('error', new Error(PLUGIN_NAME + ': File not found: ' + filePath));
continue;
}
// Create new file
file = new Vinyl({
base: targetFile.base,
path: filePath,
contents: fs.readFileSync(filePath),
stat: fs.statSync(filePath)
});
// Get new dependencies
while (dependencies = getFiles(file)) {
files = files.concat(dependencies);
}
}
// Add file itself
files.push(targetFile);
return files;
};
// Set default values
config = merge(defaults, config);
// Happy streaming
stream = Stream.Transform({
objectMode: true
});
stream._transform = function(file, unused, cb) {
var files;
if (file.isNull()) {
this.push(file);
return cb();
}
files = getFiles(file);
if (!files) {
return cb();
}
// Add dependencies and file itself to stream
files.forEach(function(file) {
this.push(file);
}.bind(this));
if (config.log) {
filesReturned = filesReturned.concat(files.map(function(file) {
return file.path;
}));
}
cb();
};
stream._flush = function(cb) {
if (config.log) {
Log('[' + AnsiColors.green(PLUGIN_NAME) + '] Files returned to stream:', filesReturned);
}
cb();
};
return stream;
};
module.exports = resolveDependencies;