#!/bin/sh NPMRC_STORE="${HOME}/.npmrcs"; NPMRC="${HOME}/.npmrc"; printUsage() { echo "" echo "Usage: npmrc [-c] [name]" } activate() { if [ ! -e "${NPMRC_STORE}/${1}" ]; then echo "Couldn't find npmrc file '${1}', looked in ${NPMRC_STORE}/${1}"; exit 1; fi; echo "Removing old .npmrc ($(readlink ${NPMRC}))"; rm -f "${NPMRC}" || exit 1; echo "Activating .npmrc '${1}'"; ln -s "${NPMRC_STORE}/${1}" "${NPMRC}" || exit 1; } if [ "${1}" = "--help" -o "${1}" = "-h" ]; then echo "npmrc" echo "" echo " Switch between different .npmrc files with ease and grace." printUsage; echo "" echo "Example:" echo " Creating and activating a new .npmrc called 'work':" echo " > touch ~/.npmrcs/work" echo " > npmrc work" exit 1 fi # init ~/.npmrc & ~/npmrc/default if [ ! -d "$NPMRC_STORE" ]; then echo "Initialising npmrc..." echo "Creating ${NPMRC_STORE}" mkdir ${NPMRC_STORE} # create default npmrc if it doesn't exist if [ ! -e "${NPMRC}" ]; then touch ${NPMRC_STORE}/default else # make existing config 'default' echo "Making ${NPMRC} the default npmrc file" mv ${NPMRC} ${NPMRC_STORE}/default fi; # use the config we just created $0 default exit $? fi if [ "x${1}" = "x" ]; then echo "Available npmrcs:"; echo; find "${NPMRC_STORE}" -type f | while read FILE; do if [ "$(readlink ${NPMRC})" = "${FILE}" ]; then echo "* $(basename ${FILE})"; else echo " $(basename ${FILE})"; fi; done; exit 0; fi; if [ "${1}" = "--create" -o "${1}" = "-c" ]; then if [ "x${2}" = "x" ]; then echo "What do you want to call your new npm configuration?" printUsage; exit 1; fi if [ -e "${NPMRC_STORE}/${2}" ]; then echo "npmrc file '${2}', already exists: ${NPMRC_STORE}/${2}"; exit 1; fi; touch ${NPMRC_STORE}/${2} activate $2 exit 0; fi if [ ! -e "${NPMRC_STORE}/${1}" ]; then FILES=$(find "${NPMRC_STORE}" -name "${1}*") # As $(echo "" | wc -l) will also return 1 we explicitly check if $FILES is empty if [ "x${FILES}" = "x" ]; then RESULTS=0 else RESULTS=$(echo "$FILES" | wc -l) fi; if [ $RESULTS = 1 ]; then $0 $(basename $FILES) exit $? elif [ $RESULTS -gt 1 ]; then echo "'${1}' is ambiguous. Found multiple matching npmrcs:" echo; echo "$FILES" | while read FILE; do if [ "$(readlink ${NPMRC})" = "${FILE}" ]; then echo "* $(basename ${FILE})"; else echo " $(basename ${FILE})"; fi; done; exit 1; fi echo "Couldn't find npmrc file '${1}', looked in ${NPMRC_STORE}/${1}"; exit 1; fi; if [ -e "${NPMRC}" ]; then if [ ! -h "${NPMRC}" ]; then echo "Current .npmrc (${NPMRC}) is not a symlink. You may want to copy it into ${NPMRC_STORE}"; exit 1; fi; if [ "$(readlink ${NPMRC})" = "${NPMRC_STORE}/${1}" ]; then echo "Current .npmrc (${NPMRC}) is already '${1}' (${NPMRC_STORE}/${1})"; exit 1; fi; fi; activate $1