11cb0ef41Sopenharmony_ci// compares the inventory of package items in the tree
21cb0ef41Sopenharmony_ci// that is about to be installed (idealTree) with the inventory
31cb0ef41Sopenharmony_ci// of items stored in the package-lock file (virtualTree)
41cb0ef41Sopenharmony_ci//
51cb0ef41Sopenharmony_ci// Returns empty array if no errors found or an array populated
61cb0ef41Sopenharmony_ci// with an entry for each validation error found.
71cb0ef41Sopenharmony_cifunction validateLockfile (virtualTree, idealTree) {
81cb0ef41Sopenharmony_ci  const errors = []
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  // loops through the inventory of packages resulted by ideal tree,
111cb0ef41Sopenharmony_ci  // for each package compares the versions with the version stored in the
121cb0ef41Sopenharmony_ci  // package-lock and adds an error to the list in case of mismatches
131cb0ef41Sopenharmony_ci  for (const [key, entry] of idealTree.entries()) {
141cb0ef41Sopenharmony_ci    const lock = virtualTree.get(key)
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    if (!lock) {
171cb0ef41Sopenharmony_ci      errors.push(`Missing: ${entry.name}@${entry.version} from lock file`)
181cb0ef41Sopenharmony_ci      continue
191cb0ef41Sopenharmony_ci    }
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    if (entry.version !== lock.version) {
221cb0ef41Sopenharmony_ci      errors.push(`Invalid: lock file's ${lock.name}@${lock.version} does ` +
231cb0ef41Sopenharmony_ci      `not satisfy ${entry.name}@${entry.version}`)
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci  return errors
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cimodule.exports = validateLockfile
30