* Given a directory that supposedly contains a package, create a PackageInfo * object and try to fill it out, EVEN IF the package.json is not readable. * Errors will then be stored in the PackageInfo for anything with the package * that might be wrong. * Because it's possible that the pa
(packageDir, pkg, isRoot)
| 427 | * package, whose dependencies we must all consider for discovery. |
| 428 | */ |
| 429 | _readPackage(packageDir, pkg, isRoot) { |
| 430 | let normalizedPackageDir = path.normalize(packageDir); |
| 431 | |
| 432 | // Most of the time, normalizedPackageDir is already a real path (i.e. fs.realpathSync |
| 433 | // will return the same value as normalizedPackageDir if the dir actually exists). |
| 434 | // Because of that, we'll assume we can test for normalizedPackageDir first and return |
| 435 | // if we find it. |
| 436 | let pkgInfo = this.getEntry(normalizedPackageDir); |
| 437 | if (pkgInfo) { |
| 438 | return pkgInfo; |
| 439 | } |
| 440 | |
| 441 | // collect errors we hit while trying to create the PackageInfo object. |
| 442 | // We'll load these into the object once it's created. |
| 443 | let setupErrors = new ErrorList(); |
| 444 | |
| 445 | // We don't already have an entry (bad or otherwise) at normalizedPackageDir. See if |
| 446 | // we can actually find a real path (including resolving links if needed). |
| 447 | let pathFailed = false; |
| 448 | |
| 449 | let realPath = getRealDirectoryPath(normalizedPackageDir); |
| 450 | |
| 451 | if (realPath) { |
| 452 | if (realPath !== normalizedPackageDir) { |
| 453 | // getRealDirectoryPath actually changed something in the path (e.g. |
| 454 | // by resolving a symlink), so see if we have this entry. |
| 455 | pkgInfo = this.getEntry(realPath); |
| 456 | if (pkgInfo) { |
| 457 | return pkgInfo; |
| 458 | } |
| 459 | } else { |
| 460 | // getRealDirectoryPath is same as normalizedPackageDir, and we know already we |
| 461 | // don't have an entry there, so we need to create one. |
| 462 | } |
| 463 | } else { |
| 464 | // no realPath, so either nothing is at the path or it's not a directory. |
| 465 | // We need to use normalizedPackageDir as the real path. |
| 466 | pathFailed = true; |
| 467 | setupErrors.addError(Errors.ERROR_PACKAGE_DIR_MISSING, normalizedPackageDir); |
| 468 | realPath = normalizedPackageDir; |
| 469 | } |
| 470 | |
| 471 | // at this point we have realPath set, we don't already have a PackageInfo |
| 472 | // for the path, and the path may or may not actually correspond to a |
| 473 | // valid directory (pathFailed tells us which). If we don't have a pkg |
| 474 | // object already, we need to be able to read one, unless we also don't |
| 475 | // have a path. |
| 476 | if (!pkg) { |
| 477 | if (!pathFailed) { |
| 478 | // we have a valid realPath |
| 479 | let packageJsonPath = path.join(realPath, PACKAGE_JSON); |
| 480 | let pkgfile = getRealFilePath(packageJsonPath); |
| 481 | if (pkgfile) { |
| 482 | try { |
| 483 | pkg = fs.readJsonSync(pkgfile); |
| 484 | } catch (e) { |
| 485 | setupErrors.addError(Errors.ERROR_PACKAGE_JSON_PARSE, pkgfile); |
| 486 | } |
no test coverage detected