Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
247 lines (202 loc) · 6.89 KB
/
Copy pathindex.js
File metadata and controls
247 lines (202 loc) · 6.89 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
240
241
242
243
244
245
246
247
import fs from 'node:fs';
import path from 'node:path';
import { debuglog } from 'node:util';
import cabinet from 'filing-cabinet';
import precinct from 'precinct';
import Config from './lib/config.js';
const debug = debuglog('tree');
/**
* Returns the dependency tree of a module as a nested object
*
* @param {import('./lib/config.js').ConfigOptions} [options]
* @returns {object}
*/
function dependencyTree(options = {}) {
const config = new Config(options);
if (!fs.existsSync(config.filename)) {
debug(`file ${config.filename} does not exist`);
return config.isListForm ? [] : {};
}
const results = config.isListForm ? traverseList(config) : traverse(config);
debug('traversal complete', results);
dedupeNonExistent(config.nonExistent);
debug('deduped list of nonExistent partials: ', config.nonExistent);
let tree;
if (config.isListForm) {
debug('list form of results requested');
tree = results;
} else {
debug('object form of results requested');
tree = {
[config.filename]: results
};
}
debug('final tree', tree);
return tree;
}
/**
* Returns a post-order flat list of absolute file paths (dependencies before dependents).
* Every file's dependencies appear at lower indices, so the root entry point is last.
* The list contains no duplicates. Accepts the same options as the default export.
*
* @param {Parameters<typeof dependencyTree>[0]} options - Same as the default export
* @returns {string[]}
*/
dependencyTree.toList = function(options = {}) {
return dependencyTree({ ...options, isListForm: true });
};
/**
* Returns resolved dependency paths for the file described by `config`.
*
* @param {Config} config
* @returns {string[]}
*/
function getDependencies(config) {
const precinctOptions = config.detectiveConfig;
precinctOptions.includeCore = false;
let dependencies;
try {
dependencies = precinct.paperwork(config.filename, precinctOptions);
debug(`extracted ${dependencies.length} dependencies: `, dependencies);
} catch(error) {
debug(`error getting dependencies: ${error.message}`);
debug(error.stack);
return [];
}
const resolvedDependencies = [];
for (const dependency of dependencies) {
const result = cabinet({
partial: dependency,
filename: config.filename,
directory: config.directory,
ast: precinct.ast,
config: config.requireConfig,
webpackConfig: config.webpackConfig,
nodeModulesConfig: config.nodeModulesConfig,
tsConfig: config.tsConfig,
tsConfigPath: config.tsConfigPath,
noTypeDefinitions: config.noTypeDefinitions
});
if (!result) {
debug(`skipping an empty filepath resolution for partial: ${dependency}`);
config.nonExistent.push(dependency);
continue;
}
const exists = fs.existsSync(result);
if (!exists) {
config.nonExistent.push(dependency);
debug(`skipping non-empty but non-existent resolution: ${result} for partial: ${dependency}`);
continue;
}
resolvedDependencies.push(result);
}
return resolvedDependencies;
}
/**
* @param {Config} config
* @returns {Object}
*/
function traverse(config) {
const subTree = {};
debug(`traversing ${config.filename}`);
if (config.visited[config.filename]) {
debug(`already visited ${config.filename}`);
return config.visited[config.filename];
}
let dependencies = getDependencies(config);
debug('cabinet-resolved all dependencies: ', dependencies);
// Eagerly mark the current file before recursing so any re-entrant visit exits early
config.visited[config.filename] = {};
if (config.filter) {
debug('using filter function to filter out dependencies');
debug(`unfiltered number of dependencies: ${dependencies.length}`);
// eslint-disable-next-line unicorn/no-array-method-this-argument, unicorn/no-array-callback-reference
dependencies = dependencies.filter(filePath => config.filter(filePath, config.filename));
debug(`filtered number of dependencies: ${dependencies.length}`);
}
for (const dependency of dependencies) {
const localConfig = config.clone();
localConfig.filename = dependency;
localConfig.directory = getLocalConfigDirectory(localConfig);
subTree[dependency] = traverse(localConfig);
}
config.visited[config.filename] = subTree;
return subTree;
}
/**
* @param {Config} config
* @returns {Array<string>}
*/
function traverseList(config) {
const result = [];
traverseListHelper(config, result);
return result;
}
/**
* @param {Config} config
* @param {Array<string>} result
*/
function traverseListHelper(config, result) {
if (config.visited[config.filename]) return;
// Eagerly mark the current file before recursing so any re-entrant visit exits early
config.visited[config.filename] = [];
let dependencies = getDependencies(config);
debug('cabinet-resolved all dependencies: ', dependencies);
if (config.filter) {
debug('using filter function to filter out dependencies');
debug(`unfiltered number of dependencies: ${dependencies.length}`);
// eslint-disable-next-line unicorn/no-array-method-this-argument, unicorn/no-array-callback-reference
dependencies = dependencies.filter(filePath => config.filter(filePath, config.filename));
debug(`filtered number of dependencies: ${dependencies.length}`);
}
for (const dependency of dependencies) {
const localConfig = config.clone();
localConfig.filename = dependency;
localConfig.directory = getLocalConfigDirectory(localConfig);
traverseListHelper(localConfig, result);
}
result.push(config.filename);
}
// Dedupe in-place so the caller's array reference stays valid
/**
* @param {string[]} nonExistent
*/
function dedupeNonExistent(nonExistent) {
const deduped = new Set(nonExistent);
nonExistent.length = deduped.size;
let i = 0;
for (const elem of deduped) {
nonExistent[i] = elem;
i++;
}
}
// If the file is in a node_modules directory, we want to resolve the root of the package,
// not the file itself, since the file may be buried in a subdirectory and not contain all
// of the package's dependencies
/**
* @param {Config} localConfig
* @returns {string}
*/
function getLocalConfigDirectory(localConfig) {
const { filename, directory } = localConfig;
if (!filename.includes('node_modules')) {
return directory;
}
const dir = path.dirname(filename);
const parts = dir.split('node_modules');
if (parts.length < 2) {
return directory;
}
const afterNodeModules = parts.pop();
if (!afterNodeModules) {
return directory;
}
const segments = afterNodeModules.split(path.sep).filter(Boolean);
if (segments.length === 0) {
return directory;
}
const packageName = segments[0].startsWith('@') ? `${segments[0]}${path.sep}${segments[1]}` : segments[0];
const projectPath = path.normalize(`${parts.join('node_modules')}node_modules${path.sep}${packageName}`);
return projectPath;
}
export default dependencyTree;