forked from jonschlinkert/module-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
239 lines (205 loc) · 5.53 KB
/
index.js
File metadata and controls
239 lines (205 loc) · 5.53 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*!
* module-tree <https://github.com/jonschlinkert/module-tree>
*
* Copyright (c) 2016-2018, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
/**
* Module dependencies
*/
const fs = require('fs');
const path = require('path');
const archy = require('archy');
const typeOf = require('kind-of');
const define = require('define-property');
const get = require('get-value');
const set = require('set-value');
const stringify = require('stringify-keys');
const yellow = require('ansi-yellow');
const mm = require('micromatch');
/**
* Build an object, where dependencies represented properties and
* keys are module names.
*
* ```js
* {
* 'union-value': {
* 'get-value': {
* pkg: [object] // package.json contents
* }
* }
* }
* ```
* @name .buildTree
* @param {Object} `pattern` Glob pattern to pass to [micromatch][] for filtering packages ([stringify-keys][] converts the object to an array of object paths, which is then filtered by micromatch)
* @param {Object} `options`
* @return {Object}
* @api public
*/
function buildTree(pattern, options) {
const opts = createOptions(pattern, options);
const pkg = require(path.resolve(opts.cwd, 'package.json'));
pattern = opts.pattern;
const cache = {
tree: {},
seen: {},
missing: {},
find: [],
lookup: function(key) {
return key && this.seen[key];
}
};
cache.tree = { [pkg.name]: resolvePkg(cache, opts.cwd) };
if (pattern) {
const isMatch = mm.matcher(pattern);
const result = {};
for (const key of stringify(cache.tree)) {
const segs = key.split('.');
let name = segs.pop();
if (isMatch(name)) {
var val = get(cache.tree, key);
if (opts.inclusive !== false) {
set(result, key, val);
}
set(result, `${key}.pkg`, val.pkg);
while (segs.length) {
copyPkg(result, cache, segs.join('.'));
segs.pop();
}
}
}
cache.tree = result;
}
return cache.tree;
}
function copyPkg(result, cache, name) {
const val = get(cache.tree, name).pkg;
if (val) {
set(result, `${name}.pkg`, val);
}
}
/**
* Build an object that can be passed to [archy][], where dependencies
* are represented as `nodes`, and the name of each package is used
* as the `label`.
*
* ```js
* // results in an object like this
* { label: 'union-value',
* nodes: [ { label: 'get-value', nodes: [] } ] }
* ```
* @name .buildNodes
* @param {Object} `tree`
* @param {Object} `options`
* @return {Object}
* @api public
*/
function buildNodes(tree, options) {
options = options || {};
function createNodes(leaf, opts) {
const nodes = [];
for (const key in leaf) {
if (key === 'pkg') continue;
const obj = {};
const val = leaf[key];
const pkg = val.pkg;
if (!pkg) {
throw new Error('cannot get package for: ' + key);
}
let suffix = opts.version ? '@' + pkg.version : '';
if (opts.author) {
const auth = author(pkg);
suffix += auth ? ' (' + auth + ') ' : '';
}
obj.label = color(key + suffix, opts);
obj.nodes = createNodes(val, opts);
nodes.push(obj);
}
return nodes;
}
return createNodes(tree, options)[0];
}
function resolvePkg(cache, dir, cwd) {
cwd = cwd || dir;
const pkgPath = path.resolve(dir, 'package.json');
const pkg = require(pkgPath);
const tree = {};
define(tree, 'pkg', pkg);
resolveDeps(cache, cwd, pkg, tree);
return tree;
}
function resolveDeps(cache, cwd, pkg, tree) {
const keys = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.devDendencies || {}));
keys.forEach(function(key) {
const pkgPath = path.resolve(cwd, 'node_modules', key);
if (!fs.existsSync(pkgPath)) {
const fn = function(key) {
return (tree[key] = cache.seen[key]);
};
cache.missing[key] = function(o) {
tree[key] = o;
};
cache.find.push(fn);
return;
}
if (!cache.seen.hasOwnProperty(key)) {
cache.seen[key] = true;
tree[key] = resolvePkg(cache, pkgPath, cwd);
}
});
}
function author(pkg) {
if (typeOf(pkg.author) === 'object') {
return pkg.author.name;
}
return pkg.author || '';
}
function color(key, options) {
const col = options && options.color;
switch (typeOf(col)) {
case 'boolean':
return col ? yellow(key) : key;
case 'function':
return col(key);
case 'string':
return mm.isMatch(key, col) ? yellow(key) : key;
default: {
return yellow(key);
}
}
}
function createOptions(pattern, options) {
if (typeof pattern !== 'string' && !Array.isArray(pattern)) {
options = pattern || options;
pattern = null;
}
const defaults = { pattern: pattern, cwd: process.cwd(), color: false };
const opts = Object.assign({}, defaults, options);
if (opts.pattern && !opts.color) {
opts.color = opts.pattern;
}
return opts;
}
/**
* Build a tree from module dependencies using [archy][].
*
* @param {Object} `pattern` Glob pattern to pass to [micromatch][] for filtering packages ([stringify-keys][] converts the object to an array of object paths, which is then filtered by micromatch)
* @param {Object} `options`
* @return {Object}
* @api public
*/
module.exports = function(pattern, options) {
const opts = createOptions(pattern, options);
const tree = buildTree(opts);
const nodes = buildNodes(tree, opts);
return archy(nodes);
};
/**
* Expose `.buildTree`
*/
module.exports.buildTree = buildTree;
/**
* Expose `.buildNodes`
*/
module.exports.buildNodes = buildNodes;