Skip to content

Commit

Permalink
Merge pull request #6251 from reactioncommerce/fix-package-link-issues
Browse files Browse the repository at this point in the history
chore: fix package link script issues
  • Loading branch information
focusaurus authored May 27, 2020
2 parents 2441bcf + 85ff882 commit 0789bd3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
11 changes: 8 additions & 3 deletions bin/package-link
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ container_copied_packages_path="/home/node/copied-packages"
container_destination_path="${container_copied_packages_path}/${package_name}"

if [[ -z "${package_path}" ]]; then
package_path="../api-plugins/${package_name}"
package_name_without_org="${package_name/#@reactioncommerce\/}"
package_path="../api-plugins/${package_name_without_org}"
full_package_path="$(cd ${package_path} && pwd)"
echo "\nUsing local package path ${full_package_path}"
echo "If this is not correct, specify the correct path as the second argument.\n"
fi

echo "Copying ${package_name} package code into container..."
docker-compose exec api sh -c "mkdir -p ${container_copied_packages_path}"
docker cp "${package_path}" "${container_id}:${container_destination_path}"
docker-compose exec api sh -c "mkdir -p ${container_destination_path}"
docker cp "${package_path}/." "${container_id}:${container_destination_path}"

# Then npm link into this project
echo "Linking package into API..."
docker-compose exec api sh -c "npm config set prefix /home/node/npm && cd /usr/local/src/app && npm link "${container_destination_path}""

# Fool nodemon into thinking something has changed so that it restarts.
# Touch first file found in /src with .js extension
echo "Restarting API..."
docker-compose exec api sh -c "touch -c $(ls ./src/*.js | head -n1)"
29 changes: 23 additions & 6 deletions bin/package-unlink
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,28 @@ IFS="$(printf "\n\t")"
# ---- End unofficial bash strict mode boilerplate

package_name=$1
package_path_in_container="./node_modules/${package_name}"
container_copied_packages_path="/home/node/copied-packages"
container_destination_path="${container_copied_packages_path}/${package_name}"

# Then npm link into this project
echo "Unlinking package from API..."
docker-compose exec api sh -c "cd /usr/local/src/app && npm unlink "${package_name}""
# We start by attempting to confirm that a package with the given name is actually
# symlinked in ./node_modules in the container. If not, we don't want to run the
# `npm unlink` command with an erroneous package name because that has odd behavior
# where it actually removed all symlinks or might even uninstall a necessary package.
# I think this is because `npm unlink` is actually just an alias for `npm uninstall`.
check_command="if [ -L "${package_path_in_container}" -a -d "${package_path_in_container}" ]; then echo -n '1'; fi"
symlink_found="$(docker-compose exec api sh -c "${check_command}")"

if [[ "${symlink_found}" == "1" ]]; then
# Then npm link into this project
echo "Unlinking package from API..."
docker-compose exec api sh -c "cd /usr/local/src/app && npm unlink ${container_destination_path}"

# Fool nodemon into thinking something has changed so that it restarts.
# Touch first file found in /src with .js extension
echo "Restarting API..."
docker-compose exec api sh -c "touch -c $(ls ./src/*.js | head -n1)"
else
echo "${package_name} does not appear to be linked"
fi

# Fool nodemon into thinking something has changed so that it restarts.
# Touch first file found in /src with .js extension
docker-compose exec api sh -c "touch -c $(ls ./src/*.js | head -n1)"
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0789bd3

Please sign in to comment.