Skip to content

Commit

Permalink
Use resolve package to implement module lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
raphinesse committed Sep 2, 2018
1 parent acb1c6e commit 3d53c2a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var events = require('cordova-common').events;
var path = require('path');
var fs = require('fs-extra');
var CordovaError = require('cordova-common').CordovaError;
const { getInstalledPath } = require('get-installed-path');
const resolve = Q.denodeify(require('resolve'));
const npa = require('npm-package-arg');
const semver = require('semver');

Expand Down Expand Up @@ -101,17 +101,19 @@ function getTargetPackageSpecFromNpmInstallOutput (npmInstallOutput) {
// is installed, rejects otherwise.
function pathToInstalledPackage (spec, dest) {
const { name, rawSpec } = npa(spec, dest);
const paths = [];
for (let p = dest; path.dirname(p) !== p; p = path.dirname(p)) {
paths.push(path.join(p, 'node_modules'));

if (!name) {
throw new CordovaError(`Cannot determine package name from spec ${spec}`);
}
return getInstalledPath(name, { local: true, paths: paths })
.then(installPath => {
const { version } = fs.readJsonSync(path.join(installPath, 'package.json'));

// We resolve the path to the module's package.json to avoid getting the
// path to `main` which could be located anywhere in the package
return resolve(path.join(name, 'package.json'), { basedir: dest })
.then(([pkgPath, { version }]) => {
if (!semver.satisfies(version, rawSpec)) {
throw new CordovaError(`Installed package ${name}@${version} does not satisfy ${name}@${rawSpec}`);
}
return installPath;
return path.dirname(pkgPath);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"dependencies": {
"cordova-common": "^2.2.0",
"fs-extra": "^6.0.1",
"get-installed-path": "^4.0.8",
"npm-package-arg": "^6.1.0",
"q": "^1.4.1",
"resolve": "^1.8.1",
"semver": "^5.5.0",
"which": "^1.3.1"
},
Expand Down

0 comments on commit 3d53c2a

Please sign in to comment.