Created
November 10, 2021 18:30
-
-
Save jsumners/ce6acf5a4e001cc3e375a3a3d277aa32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ ! -f package.json ]; then | |
# Default to npm regardless of missing package file. | |
echo "npm" | |
exit 0 | |
fi | |
NODE_VERSION=$(node --version) | |
NODE_VERSION="${NODE_VERSION%%\.*}" | |
if [ "${NODE_VERSION:1}" -lt 16 ]; then | |
# Only v16 or greater supports corepack. | |
echo "npm" | |
exit 0 | |
fi | |
HAS_PACKAGE_MANAGER=$(cat package.json | jq 'has("packageManager")') | |
if [ "${HAS_PACKAGE_MANAGER}" != "true" ]; then | |
# Package manager is not specified for the project, so npm. | |
echo "npm" | |
exit | |
fi | |
PACKAGE_MANAGER=$(cat package.json | jq -r '.packageManager') | |
PACKAGE_MANAGER=$(echo "${PACKAGE_MANAGER}" | cut -d'@' -f1) | |
corepack enable ${PACKAGE_MANAGER} | |
echo ${PACKAGE_MANAGER} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment