Last active
October 10, 2016 06:34
-
-
Save pospi/8a51b795b6c0af64a341 to your computer and use it in GitHub Desktop.
Sets up NPM module linkage when using private modules from your own registry
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# Helper script to setup symlinks and modules for services integration during development | |
# | |
# @depends http://stedolan.github.io/jq/ | |
# | |
# :WARNING: | |
# Your global npm packages must be installed for the current user in order for this script to work. | |
# If this is not the case, we recommend you nuke your global npm folder and replace with an nvm-managed | |
# node install in your homedir, which will have the correct permissions for `npm link` to function. | |
# | |
BASEPATH="$1" | |
# All dependency detection and linking is based on this value for publishConfig.registry in package.json | |
#PRIVATE_REGISTRY_URL="http://packages.yourdomain.com:4873" | |
# We have to force the registry URL globally while running `npm link` so that dependencies come via our own registry | |
if [[ -n $PRIVATE_REGISTRY_URL ]]; then | |
set -e | |
function cleanup { | |
echo "Restoring default registry..." | |
npm config delete registry # Note that if you DO use a custom registry in your daily work, you probably don't want this. | |
} | |
trap cleanup EXIT | |
echo "Forcing NPM registry to ${PRIVATE_REGISTRY_URL}..." | |
npm config set registry "${PRIVATE_REGISTRY_URL}" | |
fi | |
#------------------------------------------------------------------------------- | |
get_toplevel_packagejson_files() { | |
local basedir=$1 | |
find ${basedir} -type f -name package.json -not -path '*/node_modules/*' | |
} | |
get_packagejson_deps() { | |
local jsonfile=$1 | |
cat ${jsonfile} | jq '.dependencies | keys []' | sed 's/^"\(.*\)"$/\1/' | |
} | |
privatelyRegisteredModuleIds=() | |
#------------------------------------------------------------------------------- | |
# First loop: locate and link private packages | |
while read file; do | |
moduleDir=`dirname ${file}` | |
registryUrl=`cat ${file} | jq '.publishConfig.registry' | sed 's/^"\(.*\)"$/\1/'` | |
packageId=`cat ${file} | jq '.name' | sed 's/^"\(.*\)"$/\1/'` | |
packagePrivate=`cat ${file} | jq '.private'` | |
pushd "${moduleDir}" > /dev/null | |
# If no private registry URL set then use `private` flag to test | |
if [[ -z $PRIVATE_REGISTRY_URL ]]; then | |
if [ "$packagePrivate" == "true" ]; then | |
echo "Linking private package ${moduleDir} for development..." | |
npm link | |
# keep a list of all private packages so we can reference them into their dependencies later | |
privatelyRegisteredModuleIds+=("${packageId}") | |
fi | |
else | |
# link all private packages to our local npm registry | |
if [ "$registryUrl" == "$PRIVATE_REGISTRY_URL" ]; then | |
echo "Linking private package ${moduleDir} for development..." | |
npm link | |
# keep a list of all private packages so we can reference them into their dependencies later | |
privatelyRegisteredModuleIds+=("${packageId}") | |
fi | |
fi | |
popd > /dev/null | |
done < <(get_toplevel_packagejson_files "$BASEPATH") | |
echo "Found private packages: ${privatelyRegisteredModuleIds[@]}" | |
#------------------------------------------------------------------------------- | |
# Second loop: link private package deps and install public package deps | |
while read file; do | |
moduleDir=`dirname ${file}` | |
pushd "${moduleDir}" > /dev/null | |
# check all module dependencies for privately registered modules | |
while read dep; do | |
# link dependant private modules | |
if [[ " ${privatelyRegisteredModuleIds[@]} " =~ " ${dep} " ]]; then | |
echo "Link private dependency ${dep} to ${moduleDir}..." | |
npm link "${dep}" | |
fi | |
done < <(get_packagejson_deps './package.json') | |
# install public dependencies | |
# Note that if you do this, you might have issues with NPM3+ as it prefers to install all deps to the toplevel module. | |
# npm install | |
popd > /dev/null | |
done < <(get_toplevel_packagejson_files "$BASEPATH") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment