-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (140 loc) · 3.36 KB
/
Copy pathindex.js
File metadata and controls
160 lines (140 loc) · 3.36 KB
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
const tree = require('./lib/tree');
const cyclic = require('./lib/cyclic');
const graph = require('./lib/graph');
const log = require('./lib/log');
const defaultConfig = {
baseDir: null,
excludeRegExp: false,
fileExtensions: ['js'],
includeNpm: false,
requireConfig: null,
resolve: null,
rankdir: 'LR',
layout: 'dot',
fontName: 'Arial',
fontSize: '14px',
backgroundColor: '#111111',
nodeColor: '#c6c5fe',
nodeShape: 'box',
nodeStyle: 'rounded',
noDependencyColor: '#cfffac',
cyclicNodeColor: '#ff6c60',
edgeColor: '#757575',
graphVizOptions: false,
graphVizPath: false,
dependencyFilter: false
};
class Dependency {
/**
* Class constructor.
* @constructor
* @api public
* @param {String|Array|Object} path
* @param {Object} config
* @return {Promise}
*/
constructor (path, config) {
this.skipped = [];
if (!path) {
throw new Error('path argument not provided');
}
this.config = Object.assign({}, defaultConfig, config);
if (typeof path === 'object' && !Array.isArray(path)) {
this.tree = path;
log('using predefined tree %o', path);
return Promise.resolve(this);
}
if (typeof path === 'string') {
path = [path];
}
return tree(path, this.config).then((res) => {
this.tree = res.tree;
this.skipped = res.skipped;
return this;
});
}
/**
* Return the module dependency graph as an object.
* @api public
* @return {Object}
*/
obj () {
return this.tree;
}
/**
* Return produced warnings.
* @api public
* @return {Array}
*/
warnings () {
return {
skipped: this.skipped
};
}
/**
* Return the modules that has circular dependencies.
* @api public
* @return {Object}
*/
circular () {
return cyclic(this.tree);
}
/**
* Return a list of modules that depends on the given module.
* @api public
* @param {String} id
* @return {Array}
*/
depends (id) {
const tree = this.obj();
return Object
.keys(tree)
.filter((dep) => tree[dep].indexOf(id) >= 0);
}
/**
* Return a list of modules that no one is depending on.
* @api public
* @return {Array}
*/
orphans () {
const tree = this.obj();
const map = {};
Object
.keys(tree)
.forEach((dep) => {
tree[dep].forEach((id) => {
map[id] = true;
});
});
return Object
.keys(tree)
.filter((dep) => !map[dep]);
}
/**
* Return the module dependency graph as DOT output.
* @api public
* @return {Promise}
*/
dot () {
return graph.dot(this.obj(), this.config);
}
/**
* Write dependency graph to image.
* @api public
* @param {String} imagePath
* @return {Promise}
*/
image (imagePath) {
if (!imagePath) {
return Promise.reject(new Error('imagePath not provided'));
}
return graph.image(this.obj(), this.circular(), imagePath, this.config);
}
}
/**
* Expose API.
* @param {String|Array} path
* @param {Object} config
* @return {Promise}
*/
module.exports = (path, config) => new Dependency(path, config);