-
Notifications
You must be signed in to change notification settings - Fork 33
/
modules-map.ts
178 lines (147 loc) · 5.03 KB
/
modules-map.ts
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
import fs from 'fs';
import path from 'path';
import flattenDeep from 'lodash/flattenDeep';
import chalk from 'chalk';
import NodeModule from './node-module';
import Workspace from './workspace';
const isNotHiddenDirectory = (dirname: string) => !dirname.startsWith('.');
const isScope = (dirname: string) => dirname.startsWith('@');
export default class ModulesMap extends Map<string, Array<NodeModule>> {
root: string;
workspace: Workspace;
_yarnRequiredByAssigned: boolean;
constructor({ root, workspace }: { root: string; workspace: Workspace }) {
super();
this.root = root;
this.workspace = workspace;
this._yarnRequiredByAssigned = false;
}
addModule(name: string, nodeModule: NodeModule) {
if (!this.has(name)) {
this.set(name, []);
}
this.get(name)!.push(nodeModule);
}
getModuleOccurrences(name: string) {
const moduleOccurrences = this.get(name);
if (!moduleOccurrences) {
throw new Error(
`The node module "${name}" does not exist in ${this.root}`
);
}
return moduleOccurrences;
}
assignYarnRequiredBy(): void {
if (this._yarnRequiredByAssigned) {
return;
}
this._yarnRequiredByAssigned = true;
const yarnLock = this.workspace.yarnLock;
if (!yarnLock) {
return;
}
// Assign requiredBy using yarn lock
Object.keys(yarnLock).forEach((moduleAndVerison) => {
const moduleDependencies = yarnLock[moduleAndVerison].dependencies;
if (!moduleDependencies) {
return;
}
Object.keys(moduleDependencies).forEach((dependency) => {
const DependencyModuleOccurrences = this.get(dependency);
if (!DependencyModuleOccurrences) {
if (process.env.DEBUG === 'true') {
console.error(
chalk.dim(
`Warning: The module ${dependency} specified in yarn.lock but is not on the file system`
)
);
}
// Do not fail in this case, maybe the user is intereseted in a different module
// and this information doesn't interesting to them
return;
}
// We're only interesetd in requiredBy if the module is on the root
DependencyModuleOccurrences.forEach((nodeModule) => {
if (!nodeModule.parent) {
const moduleName = moduleAndVerison.slice(
0,
moduleAndVerison.lastIndexOf('@')
);
nodeModule.addYarnRequiredByDependency(moduleName);
}
});
});
});
// Assign requiredBy using package.json dependencies/devDependencies
this.forEach((moduleOccurrences, moduleName) => {
const workspaceDependencies = this.workspace.packageJson.dependencies;
const workspaceDevDependencies =
this.workspace.packageJson.devDependencies;
if (
workspaceDependencies &&
Object.keys(workspaceDependencies).includes(moduleName)
) {
moduleOccurrences.forEach((nodeModule) => {
// We're only interesetd in requiredBy if the module is on the root
if (!nodeModule.parent) {
nodeModule.addYarnRequiredByDependency('/');
}
});
}
if (
workspaceDevDependencies &&
Object.keys(workspaceDevDependencies).includes(moduleName)
) {
moduleOccurrences.forEach((nodeModule) => {
// We're only interesetd in requiredBy if the module is on the root
if (!nodeModule.parent) {
nodeModule.addYarnRequiredByDependency('#DEV:/');
}
});
}
});
}
static loadSync(cwd: string, workspace: Workspace): ModulesMap {
const modulesMap = new ModulesMap({ root: cwd, workspace });
function traverseNodeModules(root: string, parent?: NodeModule) {
const nodeModulesPath = path.resolve(root, 'node_modules');
if (fs.existsSync(nodeModulesPath)) {
const modulesNames = fs
.readdirSync(nodeModulesPath)
.filter(isNotHiddenDirectory);
flattenDeep(
modulesNames.map((name) => {
if (isScope(name)) {
const subScopeModules = fs.readdirSync(
path.join(nodeModulesPath, name)
);
return subScopeModules.map((subName) => {
const fullName = path.join(name, subName);
const nodeModule = new NodeModule({
nodeModulesPath,
name: fullName,
parent,
workspace,
});
modulesMap.addModule(fullName, nodeModule);
return nodeModule;
});
}
const nodeModule = new NodeModule({
nodeModulesPath,
name,
parent,
workspace,
});
modulesMap.addModule(name, nodeModule);
return nodeModule;
})
).forEach((nodeModule) =>
traverseNodeModules(nodeModule.path, nodeModule)
);
}
}
traverseNodeModules(cwd);
return modulesMap;
}
}