Skip to content

Commit 93a693b

Browse files
committed
fix: Update findModule function to search through all parents for module
1 parent f1f8a43 commit 93a693b

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

lib/tools/node-modules/node-modules-dependencies-builder.ts

+28-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from "path";
22
import { NODE_MODULES_FOLDER_NAME, PACKAGE_JSON_FILE_NAME } from "../../constants";
33

44
interface IDependencyDescription {
5+
parent: IDependencyDescription;
56
parentDir: string;
67
name: string;
78
depth: number;
@@ -20,20 +21,22 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
2021

2122
const queue: IDependencyDescription[] = _.keys(dependencies)
2223
.map(dependencyName => ({
24+
parent: null,
2325
parentDir: projectPath,
2426
name: dependencyName,
2527
depth: 0
2628
}));
2729

2830
while (queue.length) {
2931
const currentModule = queue.shift();
30-
const resolvedDependency = this.findModule(rootNodeModulesPath, currentModule.parentDir, currentModule.name, currentModule.depth, resolvedDependencies);
32+
const resolvedDependency = this.findModule(rootNodeModulesPath, currentModule, resolvedDependencies);
3133

3234
if (resolvedDependency && !_.some(resolvedDependencies, r => r.directory === resolvedDependency.directory)) {
3335
_.each(resolvedDependency.dependencies, d => {
34-
const dependency: IDependencyDescription = { name: d, parentDir: resolvedDependency.directory, depth: resolvedDependency.depth + 1 };
36+
const dependency: IDependencyDescription = { parent: currentModule, name: d, parentDir: resolvedDependency.directory, depth: resolvedDependency.depth + 1 };
3537

3638
const shouldAdd = !_.some(queue, element =>
39+
element.parent == dependency.parent &&
3740
element.name === dependency.name &&
3841
element.parentDir === dependency.parentDir &&
3942
element.depth === dependency.depth);
@@ -43,36 +46,47 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
4346
}
4447
});
4548

46-
_.each(this.getProductionDependencies(resolvedDependency.directory), d => {
47-
resolvedDependencies.push(d);
48-
});
4949
resolvedDependencies.push(resolvedDependency);
5050
}
5151
}
5252

5353
return resolvedDependencies;
5454
}
5555

56-
private findModule(rootNodeModulesPath: string, parentModulePath: string, name: string, depth: number, resolvedDependencies: IDependencyData[]): IDependencyData {
57-
let modulePath = path.join(parentModulePath, NODE_MODULES_FOLDER_NAME, name); // node_modules/parent/node_modules/<package>
58-
const rootModulesPath = path.join(rootNodeModulesPath, name);
59-
let depthInNodeModules = depth;
56+
private findModule(rootNodeModulesPath: string, depDescription: IDependencyDescription, resolvedDependencies: IDependencyData[]): IDependencyData {
57+
let modulePath = path.join(depDescription.parentDir, NODE_MODULES_FOLDER_NAME, depDescription.name); // node_modules/parent/node_modules/<package>
58+
const rootModulesPath = path.join(rootNodeModulesPath, depDescription.name);
59+
let depthInNodeModules = depDescription.depth;
6060

6161
if (!this.moduleExists(modulePath)) {
62-
modulePath = rootModulesPath; // /node_modules/<package>
63-
if (!this.moduleExists(modulePath)) {
64-
return null;
62+
63+
let moduleExists = false;
64+
let parent = depDescription.parent;
65+
66+
while(parent && !moduleExists) {
67+
modulePath = path.join(depDescription.parent.parentDir, NODE_MODULES_FOLDER_NAME, depDescription.name);
68+
moduleExists = this.moduleExists(modulePath);
69+
if (!moduleExists) {
70+
parent = parent.parent;
71+
}
6572
}
6673

74+
if (!moduleExists) {
75+
modulePath = rootModulesPath; // /node_modules/<package>
76+
if (!this.moduleExists(modulePath)) {
77+
return null;
78+
}
79+
}
80+
6781
depthInNodeModules = 0;
6882
}
6983

70-
if (_.some(resolvedDependencies, r => r.name === name && r.directory === modulePath)) {
84+
if (_.some(resolvedDependencies, r => r.name === depDescription.name && r.directory === modulePath)) {
7185
return null;
7286

7387
}
7488

75-
return this.getDependencyData(name, modulePath, depthInNodeModules);
89+
return this.getDependencyData(depDescription.name, modulePath, depthInNodeModules);
7690
}
7791

7892
private getDependencyData(name: string, directory: string, depth: number): IDependencyData {

0 commit comments

Comments
 (0)