• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
7function validateLockfile (virtualTree, idealTree) {
8  const errors = []
9
10  // loops through the inventory of packages resulted by ideal tree,
11  // for each package compares the versions with the version stored in the
12  // package-lock and adds an error to the list in case of mismatches
13  for (const [key, entry] of idealTree.entries()) {
14    const lock = virtualTree.get(key)
15
16    if (!lock) {
17      errors.push(`Missing: ${entry.name}@${entry.version} from lock file`)
18      continue
19    }
20
21    if (entry.version !== lock.version) {
22      errors.push(`Invalid: lock file's ${lock.name}@${lock.version} does ` +
23      `not satisfy ${entry.name}@${entry.version}`)
24    }
25  }
26  return errors
27}
28
29module.exports = validateLockfile
30