Skip to content

Commit

Permalink
feat: read yalc packages file for bulk linking
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Murray <[email protected]>
  • Loading branch information
mikemurray committed Jul 1, 2020
1 parent 7ea0c38 commit e1447c2
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions bin/package-link
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,67 @@ IFS="$(printf "\n\t")"

package_name=$1
package_path=$2
yalc_packages_file_name="yalc-packages"
container_id="$(docker-compose ps -q api)"
container_copied_packages_path="/home/node/copied-packages"
container_destination_path="${container_copied_packages_path}/${package_name}"

# Check and install yalc if necessary
if ! [[ -x "$(command -v yalc)" ]]; then
echo "yalc not installed in container. Installing now.\n"
docker-compose exec api sh -c "npm i -g yalc"
fi
if [[ -z "${package_name}" ]]; then
if ! [[ -f "$yalc_packages_file_name" ]]; then
echo "Yalc packages file doesn't exist. Creating..."
cp "${yalc_packages_file_name}.example" ${yalc_packages_file_name}
fi

if [[ -z "${package_path}" ]]; then
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 "Reading package list from ${yalc_packages_file_name}"

while IFS== read -r path val; do
package_path="$path"
package_name="$(node -p -e "JSON.parse(process.argv[1]).name" "$(cat ${package_path}/package.json)")"
is_enabled="$val"
# echo $package_name

if [[ $package_name ]]; then
if [[ $is_enabled == "true" ]]; then
container_destination_path="${container_copied_packages_path}/${package_name}"

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

echo "Copying `${package_path}` package code into container..."
docker-compose exec api sh -c "mkdir -p ${container_destination_path}"
docker cp "${package_path}/." "${container_id}:${container_destination_path}"
# Then yalc link into this project
echo "Linking package ${package_name} into API..."
docker exec ${container_id} sh -c "cd ${container_destination_path} && yalc push"
docker exec ${container_id} sh -c "cd /usr/local/src/app && yalc link ${package_name}"
else
echo "Unlinking package ${package_name} from API..."
docker exec ${container_id} sh -c "cd /usr/local/src/app && yalc remove ${package_name}"
docker exec ${container_id} sh -c "rm -rf /usr/local/src/app/node_modules/${package_name}"
fi
fi
done < "$yalc_packages_file_name"

# Then yalc link into this project
echo "Linking package into API..."
docker-compose exec api sh -c "cd ${container_destination_path} && yalc push"
docker-compose exec api sh -c "cd /usr/local/src/app && yalc link "${package_name}""
echo "Updating packages..."
docker exec ${container_id} sh -c "cd /usr/local/src/app && npm i"
docker exec ${container_id} sh -c "cd /usr/local/src/app && yalc update"
else
container_destination_path="${container_copied_packages_path}/${package_name}"

if [[ -z "${package_path}" ]]; then
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_path}` package code into container..."
docker-compose exec api sh -c "mkdir -p ${container_destination_path}"
docker cp "${package_path}/." "${container_id}:${container_destination_path}"

# Then yalc link into this project
echo "Linking package into API..."
docker-compose exec api sh -c "cd ${container_destination_path} && yalc push"
docker-compose exec api sh -c "cd /usr/local/src/app && yalc link ${package_name}"
fi

# Fool nodemon into thinking something has changed so that it restarts.
# Touch first file found in /src with .js extension
Expand Down

0 comments on commit e1447c2

Please sign in to comment.