Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit 1c5f565

Browse files
author
estebanlopez
committed
Merge branch 'fix-package-load' into v1.0.0
2 parents dd6c90d + 4fcb722 commit 1c5f565

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

lib/packageTools.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,35 @@ function downloadFile (inputUri, callback) {
6262
}
6363

6464
// returns package information (package.json) given a package name
65-
function getPackageInformation (packageName) {
65+
function getPackageInformation (packageName, parentPackagePath) {
6666

6767
try {
68-
var packagePath = path.dirname(require.main.filename);
69-
var modulesPath = path.join(packagePath, node_modules);
70-
71-
try { fs.statSync(modulesPath).isDirectory(); }
72-
catch (er) {
73-
modulesPath = path.resolve(packagePath, '..');
68+
if (!parentPackagePath) {
69+
parentPackagePath = path.dirname(require.main.filename);
7470
}
7571

72+
var modulePath = parentPackagePath;
73+
7674
if (packageName) {
77-
packagePath = path.join(modulesPath, packageName);
75+
modulePath = modulePath.endsWith(node_modules) ? modulePath : path.join(modulePath, node_modules);
76+
modulePath = path.join(modulePath, packageName);
7877
}
78+
modulePath = path.join(modulePath, packageJson);
7979

80-
packagePath = path.join(packagePath, packageJson);
81-
82-
return require(packagePath);
80+
return require(modulePath);
8381
}
8482
catch (err) {
85-
throw new Error('Error retrieving information for module: \'' + (packageName || 'main') + '\'.');
83+
if (err.code !== 'MODULE_NOT_FOUND') {
84+
throw err;
85+
}
86+
87+
var next = path.resolve(parentPackagePath, '..');
88+
if (parentPackagePath !== next) {
89+
return getPackageInformation(packageName, next);
90+
}
8691
}
92+
93+
throw new Error('Error retrieving information for module: \'' + (packageName || 'main') + '\'.');
8794
}
8895

8996
// returns package information (package.json) given a file or directory

0 commit comments

Comments
 (0)