Skip to content

Instantly share code, notes, and snippets.

@RickyCook
Last active February 8, 2018 02:55
Show Gist options
  • Save RickyCook/c7fcf98386ea8885d8588db96e4a01e8 to your computer and use it in GitHub Desktop.
Save RickyCook/c7fcf98386ea8885d8588db96e4a01e8 to your computer and use it in GitHub Desktop.
Clone truffle repos, and interlink them with all their node_modules for a full local dev environment
#!/bin/bash
set -e
this_dir="$(cd "$(dirname "$0")"; pwd)"
script_name="$0"
repo_prefix='https://github.com/trufflesuite/'
repo_suffix='.git'
root_repo='truffle-core'
function error() {
# Output a message to stderr
echo $1 >&2
}
function die() {
# Show an error message, then exit
error "$1"
exit ${2:-1}
}
function usage() {
# Print the usage message
echo "Usage $script_name [install | clean]"
}
function repo() {
# Get a full repo URL from just the name
echo "$repo_prefix$1$repo_suffix"
}
function clone() {
# Clone the given repo name into the working dir
git clone "$(repo "$1")" "$this_dir/$1"
}
function ensure_repo() {
# Ensure that a repo exists by cloning it if it doesn't
repo="$1"
[[ -d "$this_dir/$repo" ]] || clone "$repo"
}
function ensure_all_repos() {
# Ensure that a repo exists, and then check that repos package.json for
# truffle dependencies, triggering ensure_all_repos for any that don't exist
# yet
repo="$1"
ensure_repo "$repo"
(cd "$this_dir/$repo";
deps=($(jq -r '.dependencies | with_entries(select(.key | startswith("truffle-"))) | keys | .[]' package.json || true))
for dep_repo in "${deps[@]}"; do
if [[ ! -d "$this_dir/$dep_repo" ]]; then
ensure_all_repos "$dep_repo"
fi
done
)
}
function install_repo() {
# NPM install for a repo name
echo "Installing $1"
repo="$1"; (cd "$this_dir/$repo";
npm install
)
}
function link_repo() {
# Replace all packages in a repo's node_modules directery with symlinks to
# their local checkout
echo "Linking $1"
repo="$1"
[[ -d "$this_dir/$repo/node_modules" ]] || return 0
(cd "$this_dir/$repo/node_modules";
dirs=($(ls | grep truffle- || true))
for repo in "${dirs[@]}"; do
rm -rf "$repo"
ln -s "../../$repo"
done
)
}
function install_and_link_repo() {
# Shortcut for install_repo and link_repo
repo="$1"
install_repo "$repo"
link_repo "$repo"
}
function cmd_install() {
# Root for the install command. Ensures that truffle-core exists, then
# recursively checks out all deps of truffle repos, then install and link
# them all together
ensure_repo "$root_repo"
echo "Cloning repos"
dirs=($(ls | grep truffle))
for dir in "${dirs[@]}"; do
ensure_all_repos "$dir"
done
dirs=($(ls | grep truffle))
for dir in "${dirs[@]}"; do
install_and_link_repo "$dir"
done
}
function cmd_clean() {
# Clean up by purging all node_modules directories
dirs=($(ls | grep truffle))
for dir in "${dirs[@]}"; do
if [[ -d "$dir/node_modules" ]]; then
echo "Cleaning $dir"
(
cd "$dir/node_modules"
rm -rf truffle*
)
fi
done
}
(
cd "$this_dir"
case "$1" in
install) cmd_install ;;
clean) cmd_clean ;;
*)
usage
if [[ -z "$1" ]]; then
die "No commmand given"
else
die "Unknown command: $1"
fi
;;
esac
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment