Skip to content

Commit 1fc817f

Browse files
committed
fix: check for not installed dependencies using require.resolve
1 parent bfa8e91 commit 1fc817f

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

lib/services/plugins-service.ts

+18-28
Original file line numberDiff line numberDiff line change
@@ -276,40 +276,30 @@ export class PluginsService implements IPluginsService {
276276
public async ensureAllDependenciesAreInstalled(
277277
projectData: IProjectData
278278
): Promise<void> {
279-
let installedDependencies = this.$fs.exists(
280-
this.getNodeModulesPath(projectData.projectDir)
281-
)
282-
? this.$fs.readDirectory(this.getNodeModulesPath(projectData.projectDir))
283-
: [];
284-
// Scoped dependencies are not on the root of node_modules,
285-
// so we have to list the contents of all directories, starting with @
286-
// and add them to installed dependencies, so we can apply correct comparison against package.json's dependencies.
287-
_(installedDependencies)
288-
.filter((dependencyName) => _.startsWith(dependencyName, "@"))
289-
.each((scopedDependencyDir) => {
290-
const contents = this.$fs.readDirectory(
291-
path.join(
292-
this.getNodeModulesPath(projectData.projectDir),
293-
scopedDependencyDir
294-
)
295-
);
296-
installedDependencies = installedDependencies.concat(
297-
contents.map(
298-
(dependencyName) => `${scopedDependencyDir}/${dependencyName}`
299-
)
300-
);
301-
});
302-
303279
const packageJsonContent = this.$fs.readJson(
304280
this.getPackageJsonFilePath(projectData.projectDir)
305281
);
306282
const allDependencies = _.keys(packageJsonContent.dependencies).concat(
307283
_.keys(packageJsonContent.devDependencies)
308284
);
309-
const notInstalledDependencies = _.difference(
310-
allDependencies,
311-
installedDependencies
312-
);
285+
286+
const notInstalledDependencies = allDependencies.map(dep => {
287+
try {
288+
this.$logger.trace(`Checking if ${dep} is installed...`);
289+
require.resolve(`${dep}/package.json`, {
290+
paths: [projectData.projectDir]
291+
})
292+
293+
// return false if the dependency is installed - we'll filter out boolean values
294+
// and end up with an array of dep names that are not installed if we end up
295+
// inside the catch block.
296+
return false;
297+
} catch(err) {
298+
this.$logger.trace(`Error while checking if ${dep} is installed. Error is: `, err)
299+
return dep;
300+
}
301+
}).filter(Boolean);
302+
313303
if (this.$options.force || notInstalledDependencies.length) {
314304
this.$logger.trace(
315305
"Npm install will be called from CLI. Force option is: ",

0 commit comments

Comments
 (0)