forked from daniele-rapagnani/gulp-walker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (49 loc) · 1.77 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
var through = require('through2');
var gutil = require('gulp-util');
var extend = require('extend');
var path = require('path');
var vinylFile = require('vinyl-file');
var StringDecoder = require('string_decoder').StringDecoder;
var Analyzer = require('./lib/Analyzer');
var AnalyzerState = require('./lib/AnalyzerState');
var Logger = require('./lib/Logger');
var analyzerState = new AnalyzerState();
module.exports = function(config) {
config = extend(true, {
stopperOnFirstRun: true,
debug: false
}, config);
Logger.level = config.debug === false ? Logger.WARN : config.debug;
var analyzer = new Analyzer(config, analyzerState);
return through.obj(function(file, enc, cb) {
var decoder = new StringDecoder(enc);
var content = decoder.write(file.contents);
var fileUnprocessed = analyzer.isUnprocessedFile(file.path);
analyzer.registerFileBase(file);
var dependencies = analyzer.resolveDependencies(content, file.path);
if (!fileUnprocessed) {
Logger.debug("Pushing dependencies for", file.path, dependencies);
for (var i = 0; i < dependencies.length; i++) {
var dependency = dependencies[i];
var base = analyzer.getFileBase(dependency);
if (!base) {
Logger.warn("The file '"+file.path+"' wasn't analyzed the first time");
continue;
}
var dependencyFile = vinylFile.readSync(dependency, {
cwd: file.cwd,
base: base
});
this.push(dependencyFile);
}
}
if (!(fileUnprocessed && config.stopperOnFirstRun)) {
this.push(file);
} else {
Logger.debug("Stopped file from going further down the stream.\n"+
"This happened because this was the first time the file was processed.\n"+
"If this is not the desired behavior set the 'stopperOnFirstRun' option to false");
}
cb();
});
};