#!/bin/sh
set -e

check_yarn_install_is_idempotent() {
  yarn install
  if ! git diff --exit-code yarn.lock; then
    echo "Lock file is not stable. See above for diff." >&2
    echo "To fix this, run the following and commit the result:" >&2
    echo "yarn && yarn yarn-deduplicate --strategy fewer && yarn" >&2
    exit 1
  fi
}

# We need to sandwich yarn-deduplicate with yarn install calls.
# This is how Renovate does it:
# https://gitlab.com/gitlab-org/frontend/renovate-fork/-/blob/f7745bc34724f01e2d953cfb047c706d0e3f7fc4/lib/modules/manager/npm/post-update/yarn.ts#L251-254
check_yarn_install_is_idempotent
yarn run yarn-deduplicate --strategy fewer --list --fail
check_yarn_install_is_idempotent
