-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathparsers.ts
More file actions
104 lines (96 loc) · 3.32 KB
/
parsers.ts
File metadata and controls
104 lines (96 loc) · 3.32 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
import { ICruiseResult } from 'dependency-cruiser';
import { Module, ModuleDeps, ModuleImportMap } from './types';
export function parseModuleDeps(result: ICruiseResult): ModuleDeps {
const localModules = new Set<string>();
const nodeBuiltInModules = new Set<string>();
const aliases = new Map<string, string>();
const npmPackageNames = new Map<string, string>();
const sourceDeps = new Map<
string,
{ source: string; isDynamic: boolean }[]
>();
const sourceImportedBy = new Map<
string,
{ source: string; isDynamic: boolean }[]
>();
result.summary.optionsUsed.args?.split(/\s+/).forEach(entryPoint => {
localModules.add(entryPoint);
});
result.modules.forEach(module => {
module.dependencies.forEach(dependency => {
sourceDeps.set(module.source, [
...(sourceDeps.get(module.source) ?? []),
{ source: dependency.resolved, isDynamic: dependency.dynamic },
]);
sourceImportedBy.set(dependency.resolved, [
...(sourceImportedBy.get(dependency.resolved) ?? []),
{ source: module.source, isDynamic: dependency.dynamic },
]);
if (dependency.dependencyTypes.includes('local')) {
localModules.add(dependency.resolved);
}
if (dependency.dependencyTypes.includes('core')) {
nodeBuiltInModules.add(dependency.resolved);
}
if (dependency.dependencyTypes.includes('aliased')) {
aliases.set(dependency.resolved, dependency.module);
localModules.add(dependency.resolved);
} else if (
dependency.dependencyTypes.some(type => type.startsWith('npm'))
) {
npmPackageNames.set(dependency.resolved, dependency.module);
}
});
});
const allModules = result.modules
.map((module): Module => {
const npmPackageName = npmPackageNames.get(module.source);
const alias = aliases.get(module.source);
const isLocal = localModules.has(module.source);
const isNodeBuiltIn = nodeBuiltInModules.has(module.source);
return {
path: npmPackageName ?? module.source,
source: module.source,
isLocal,
isNodeBuiltIn,
...(alias && { alias }),
};
})
.filter(
(item, index, array) =>
array.findIndex(({ path }) => path === item.path) === index,
)
.sort((a, b) => a.path.localeCompare(b.path));
const { moduleBySource, moduleByPath } = allModules.reduce<{
moduleBySource: Record<string, Module>;
moduleByPath: Record<string, Module>;
}>(
(acc, module) => ({
moduleBySource: { ...acc.moduleBySource, [module.source]: module },
moduleByPath: { ...acc.moduleByPath, [module.path]: module },
}),
{ moduleBySource: {}, moduleByPath: {} },
);
const pathDeps: ModuleImportMap = {};
sourceDeps.forEach((value, key) => {
pathDeps[moduleBySource[key].path] = value.map(({ source, isDynamic }) => ({
path: moduleBySource[source].path,
isDynamic,
}));
});
const pathImportedBy: ModuleImportMap = {};
sourceImportedBy.forEach((value, key) => {
pathImportedBy[moduleBySource[key].path] = value.map(
({ source, isDynamic }) => ({
path: moduleBySource[source].path,
isDynamic,
}),
);
});
return {
modules: moduleByPath,
paths: allModules.map(({ path }) => path),
deps: pathDeps,
importedBy: pathImportedBy,
};
}