Skip to content

Commit 42a8898

Browse files
committed
refactor: production dependency discovery
WIP - tests need a rewrite because require.resolve can't easily be mocked.
1 parent e62784e commit 42a8898

File tree

1 file changed

+85
-62
lines changed

1 file changed

+85
-62
lines changed

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

Lines changed: 85 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export class NodeModulesDependenciesBuilder
4848
const resolvedDependency = this.findModule(
4949
rootNodeModulesPath,
5050
currentModule,
51-
resolvedDependencies
51+
resolvedDependencies,
52+
projectPath
5253
);
5354

5455
if (
@@ -92,56 +93,78 @@ export class NodeModulesDependenciesBuilder
9293
private findModule(
9394
rootNodeModulesPath: string,
9495
depDescription: IDependencyDescription,
95-
resolvedDependencies: IDependencyData[]
96+
resolvedDependencies: IDependencyData[],
97+
projectPath: string,
9698
): IDependencyData {
97-
let modulePath = path.join(
98-
depDescription.parentDir,
99-
NODE_MODULES_FOLDER_NAME,
100-
depDescription.name
101-
); // node_modules/parent/node_modules/<package>
102-
const rootModulesPath = path.join(rootNodeModulesPath, depDescription.name);
103-
let depthInNodeModules = depDescription.depth;
104-
105-
if (!this.moduleExists(modulePath)) {
106-
let moduleExists = false;
107-
let parent = depDescription.parent;
108-
109-
while (parent && !moduleExists) {
110-
modulePath = path.join(
111-
depDescription.parent.parentDir,
112-
NODE_MODULES_FOLDER_NAME,
113-
depDescription.name
114-
);
115-
moduleExists = this.moduleExists(modulePath);
116-
if (!moduleExists) {
117-
parent = parent.parent;
118-
}
119-
}
120-
121-
if (!moduleExists) {
122-
modulePath = rootModulesPath; // /node_modules/<package>
123-
if (!this.moduleExists(modulePath)) {
124-
return null;
125-
}
126-
}
127-
128-
depthInNodeModules = 0;
129-
}
130-
131-
if (
132-
_.some(
133-
resolvedDependencies,
134-
(r) => r.name === depDescription.name && r.directory === modulePath
135-
)
136-
) {
137-
return null;
138-
}
139-
140-
return this.getDependencyData(
141-
depDescription.name,
142-
modulePath,
143-
depthInNodeModules
144-
);
99+
try {
100+
const modulePath = require.resolve(`${depDescription.name}/package.json`, {
101+
paths: [projectPath]
102+
}).replace('/package.json', '')
103+
104+
// if we already resolved this dependency, we return null to avoid a duplicate resolution
105+
if(resolvedDependencies.some(r => {
106+
return r.name === depDescription.name && r.directory === modulePath
107+
})) {
108+
return null;
109+
}
110+
111+
return this.getDependencyData(
112+
depDescription.name,
113+
modulePath,
114+
depDescription.depth
115+
);
116+
} catch (err) {
117+
return null;
118+
}
119+
120+
// let modulePath = path.join(
121+
// depDescription.parentDir,
122+
// NODE_MODULES_FOLDER_NAME,
123+
// depDescription.name
124+
// ); // node_modules/parent/node_modules/<package>
125+
// const rootModulesPath = path.join(rootNodeModulesPath, depDescription.name);
126+
// let depthInNodeModules = depDescription.depth;
127+
//
128+
// if (!this.moduleExists(modulePath)) {
129+
// let moduleExists = false;
130+
// let parent = depDescription.parent;
131+
//
132+
// while (parent && !moduleExists) {
133+
// modulePath = path.join(
134+
// depDescription.parent.parentDir,
135+
// NODE_MODULES_FOLDER_NAME,
136+
// depDescription.name
137+
// );
138+
// moduleExists = this.moduleExists(modulePath);
139+
// if (!moduleExists) {
140+
// parent = parent.parent;
141+
// }
142+
// }
143+
//
144+
// if (!moduleExists) {
145+
// modulePath = rootModulesPath; // /node_modules/<package>
146+
// if (!this.moduleExists(modulePath)) {
147+
// return null;
148+
// }
149+
// }
150+
//
151+
// depthInNodeModules = 0;
152+
// }
153+
//
154+
// if (
155+
// _.some(
156+
// resolvedDependencies,
157+
// (r) => r.name === depDescription.name && r.directory === modulePath
158+
// )
159+
// ) {
160+
// return null;
161+
// }
162+
//
163+
// return this.getDependencyData(
164+
// depDescription.name,
165+
// modulePath,
166+
// depthInNodeModules
167+
// );
145168
}
146169

147170
private getDependencyData(
@@ -175,18 +198,18 @@ export class NodeModulesDependenciesBuilder
175198
return null;
176199
}
177200

178-
private moduleExists(modulePath: string): boolean {
179-
try {
180-
let modulePathLsStat = this.$fs.getLsStats(modulePath);
181-
if (modulePathLsStat.isSymbolicLink()) {
182-
modulePathLsStat = this.$fs.getLsStats(this.$fs.realpath(modulePath));
183-
}
184-
185-
return modulePathLsStat.isDirectory();
186-
} catch (e) {
187-
return false;
188-
}
189-
}
201+
// private moduleExists(modulePath: string): boolean {
202+
// try {
203+
// let modulePathLsStat = this.$fs.getLsStats(modulePath);
204+
// if (modulePathLsStat.isSymbolicLink()) {
205+
// modulePathLsStat = this.$fs.getLsStats(this.$fs.realpath(modulePath));
206+
// }
207+
//
208+
// return modulePathLsStat.isDirectory();
209+
// } catch (e) {
210+
// return false;
211+
// }
212+
// }
190213
}
191214

192215
injector.register(

0 commit comments

Comments
 (0)