validate-lockfile.js 1023 B

1234567891011121314151617181920212223242526272829
  1. // compares the inventory of package items in the tree
  2. // that is about to be installed (idealTree) with the inventory
  3. // of items stored in the package-lock file (virtualTree)
  4. //
  5. // Returns empty array if no errors found or an array populated
  6. // with an entry for each validation error found.
  7. function validateLockfile (virtualTree, idealTree) {
  8. const errors = []
  9. // loops through the inventory of packages resulted by ideal tree,
  10. // for each package compares the versions with the version stored in the
  11. // package-lock and adds an error to the list in case of mismatches
  12. for (const [key, entry] of idealTree.entries()) {
  13. const lock = virtualTree.get(key)
  14. if (!lock) {
  15. errors.push(`Missing: ${entry.name}@${entry.version} from lock file`)
  16. continue
  17. }
  18. if (entry.version !== lock.version) {
  19. errors.push(`Invalid: lock file's ${lock.name}@${lock.version} does ` +
  20. `not satisfy ${entry.name}@${entry.version}`)
  21. }
  22. }
  23. return errors
  24. }
  25. module.exports = validateLockfile