Files
plugin-drone-npm-auth/bin/npm-auth
T
Andy Trevorah ab05668267 fix plugin trashing existing .npmrc
The fix is to append the contents of ~/.npmrc to the local /npmrc rather than overwrite it.

Also, this fixes an issue where only the first line would be appended (`always-auth` etc was getting lost). This was caused by echo only printing to the first carriage return. I've also stopped replacing whitespace with newlines as it seems error prone.
2019-01-14 16:16:01 +00:00

52 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
starttime=$(date +%s.%N)
username="${NPM_USERNAME:-$PLUGIN_USERNAME}"
password="${NPM_PASSWORD:-$PLUGIN_PASSWORD}"
email="${NPM_EMAIL:-$PLUGIN_EMAIL}"
registry="${NPM_REGISTRY:-$PLUGIN_REGISTRY}"
scope="${NPM_SCOPE:-$PLUGIN_SCOPE}"
path="${PLUGIN_PATH:-./}"
token="${NPM_TOKEN:-$PLUGIN_TOKEN}"
set -e
echo "-- Setting up authentication from NPM..."
if [ -z "$registry" ]; then
registry="registry.npmjs.org"
fi
if [ -z "$token" ]; then
if [ -z "$username" ]; then
echo "-- Username is not set!"
exit 1
fi
if [ -z "$password" ]; then
echo "-- Password is not set!"
exit 1
fi
if [ -z "$email" ]; then
echo "-- Email is not set!"
exit 1
fi
if [ -z "$scope" ]; then
scope_option=""
else
scope_option="--scope=$scope"
fi
expect-npm-auth "npm login --registry $registry $scope_option" $username $password $email >/dev/null 2>&1
else
echo "//$registry/:_authToken=$token" > ~/.npmrc
fi
endtime=$(date +%s.%N)
echo "duration: $(echo "$endtime $starttime" | awk '{printf "%f", $1 - $2}')s"
if [ ! -e ~/.npmrc ]; then
echo "-- Error logging into NPM"
exit 1
else
cat ~/.npmrc >> $path.npmrc
echo "-- NPM authentication done!"
fi