-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileDependenciesTracker.js
55 lines (49 loc) · 1.73 KB
/
FileDependenciesTracker.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
var path = require('path');
function FileDependenciesTracker(appModule) {
this.moduleDeps = {};
this.directoriesWithModule = {};
this.appModule = appModule;
}
FileDependenciesTracker.prototype.directoryIsRequiredByModule = function (module, dir, cdc) {
var moduleDep = this.moduleDeps[module];
if (cdc.indexOf(module) !== cdc.length - 1) {
console.warn("Module", module, "was found in a dependency cycle", cdc);
return false;
}
if (!moduleDep) {
// this is an unknown module, ngSanitze for example
return false;
} else if (moduleDep.directory === dir) {
return true;
} else {
return moduleDep.dependencies.some(function (dep) {
cdc.push(dep);
var dependencyResult = this.directoryIsRequiredByModule(dep, dir, cdc);
cdc.pop();
return dependencyResult;
}.bind(this));
}
};
FileDependenciesTracker.prototype.register = function (module, directory, moduleDependencies) {
this.moduleDeps[module] = {
module: module,
directory: directory,
dependencies: moduleDependencies
};
this.directoriesWithModule[directory] = module;
};
FileDependenciesTracker.prototype.closestDirWithModule = function (filePath) {
var dirName = path.dirname(filePath);
if (!dirName || dirName === filePath) {
return null;
}
if (this.directoriesWithModule[dirName]) {
return dirName;
} else {
return this.closestDirWithModule(dirName);
}
};
FileDependenciesTracker.prototype.fileIsRequired = function (filePath) {
return this.directoryIsRequiredByModule(this.appModule, this.closestDirWithModule(filePath), [this.appModule]);
};
module.exports = FileDependenciesTracker;