Skip to content

Instantly share code, notes, and snippets.

@sagarpanchal
Last active March 2, 2026 16:12
Show Gist options
  • Select an option

  • Save sagarpanchal/240beba3c42994ed18facc39edb416d3 to your computer and use it in GitHub Desktop.

Select an option

Save sagarpanchal/240beba3c42994ed18facc39edb416d3 to your computer and use it in GitHub Desktop.
Npm Aws code-artifact integration
function chrome-unsafe-mode {
local os
os="$(uname -s)"
case "$os" in
Darwin*) open -n -a "Google Chrome" --args --user-data-dir="$HOME/chrome-unsafe-mode" --disable-web-security ;;
Linux*) google-chrome --user-data-dir="$HOME/chrome-unsafe-mode" --disable-web-security ;;
esac
}
# Logs into AWS SSO and CodeArtifact.
#
# This function expects the following details to be present in the
# $HOME/.aws/config file:
#
# - sso_session: The SSO session name.
# - profile: The profile name.
# - sso_account_id: The AWS account ID.
# - region: The region where the CodeArtifact repository is located.
#
# The function will exit with a non-zero code if any of the above
# details are not present in the config file.
function aws-login {
local CONFIG_FILE="$HOME/.aws/config"
local SSO_SESSION
SSO_SESSION=$(awk -F ' = ' '/^\[sso-session / {gsub(/^\[sso-session |\]$/, "", $0); print; exit}' "$CONFIG_FILE")
local PROFILE
PROFILE=$(awk -F ' = ' '/^\[profile / {gsub(/^\[profile |\]$/, "", $0); print; exit}' "$CONFIG_FILE")
local ACCOUNT_ID
ACCOUNT_ID=$(awk -F ' = ' '$1 == "sso_account_id" {print $2}' "$CONFIG_FILE")
local REGION
REGION=$(awk -F ' = ' '$1 == "region" {print $2}' "$CONFIG_FILE")
if [[ -z "$SSO_SESSION" || -z "$PROFILE" || -z "$ACCOUNT_ID" || -z "$REGION" ]]; then
echo "[ERROR] Could not extract required AWS SSO and profile details."
return 1
fi
# Run aws sso login
echo "[INFO] aws sso login"
aws sso login --sso-session "$SSO_SESSION"
# Run aws codeartifact login
echo "[INFO] aws codeartifact login"
aws codeartifact login --tool npm --repository NugetNPMRepo --domain oceantg --domain-owner "$ACCOUNT_ID" --profile "$PROFILE" --region "$REGION"
}
# Wraps commands to handle aws codeartifact 401 errors by retrying with AWS login.
#
# Args:
# $1: The command to wrap (npm, npx, ncu, etc.)
# $@: The remaining arguments to pass to the command
function npm-wrapper {
local cmd=$1
shift # Remove the first argument (npm or npx) and pass the rest
# Create a unique temporary file for output
local tmp
tmp=$(mktemp /tmp/"${cmd}"_last_output.XXXXXX)
local exit_code
script -q /dev/null command "$cmd" "$@" | tee "$tmp"
exit_code=${pipestatus[1]}
# Read and remove the temporary file
local output
output=$(<"$tmp")
rm "$tmp"
# If an error occurred and the output includes "error code E401", run aws-login and retry
if [[ $output == *"E401"* ]]; then
echo "[INFO] 401 detected. Running aws-login"
aws-login
script -q /dev/null command "$cmd" "$@"
exit_code=$?
fi
return "$exit_code"
}
function npm { npm-wrapper npm "$@"; }
function npx { npm-wrapper npx "$@"; }
function ncu { npm-wrapper ncu "$@"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment