Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
fix: prevent the builds from failing for public projects that use yar…
Browse files Browse the repository at this point in the history
…n 2 or above with workspaces
  • Loading branch information
fulopkovacs committed Mar 28, 2022
1 parent 7335e6b commit 536caf3
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions run-build-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,21 @@ install_deps() {
restore_node_modules() {
local workspace_output
local workspace_exit_code
local workspace_errors_logfile
local workspace_error_message
local package_manager_version
local package_locations
local installer=$1
# YARN_IGNORE_PATH will ignore the presence of a local yarn executable (i.e. yarn 2) and default
# to using the global one (which, for now, is always yarn 1.x). See https://yarnpkg.com/configuration/yarnrc#ignorePath
# we can actually use this command for npm workspaces as well
workspace_output="$(YARN_IGNORE_PATH=1 yarn workspaces --json info 2>/dev/null)"
workspace_errors_logfile="yarn-workspace-errors-log.json"
workspace_output="$(YARN_IGNORE_PATH=1 yarn workspaces --json info 2>"$workspace_errors_logfile")"
workspace_exit_code=$?

if [ $workspace_exit_code -eq 0 ]
then
echo "$installer workspaces detected"
local package_locations
# Extract all the packages and respective locations. .data will be a JSON object like
# .data will be a JSON object like
# {
# "my-package-1": {
# "location": "packages/blog-1",
Expand All @@ -109,7 +113,42 @@ restore_node_modules() {
# (...)
# }
# We need to cache all the node_module dirs, or we'll always be installing them on each run
mapfile -t package_locations <<< "$(echo "$workspace_output" | jq -r '.data | fromjson | to_entries | .[].value.location')"
mapfile -t package_locations <<<"$(echo "$workspace_output" | jq -r '.data | fromjson | to_entries | .[].value.location')"
else
# In yarn 1.x workspaces only work on private repos.
# See https://classic.yarnpkg.com/lang/en/docs/workspaces/#toc-how-to-use-it
# we have to check the error message to see if the repo is public and uses
# yarn 2 (or greater) with workspaces
if [ $workspace_exit_code -eq 1 ]
then
workspace_error_message=$(jq -r ".data" "$workspace_errors_logfile" 2>/dev/null)
package_manager_version=$(yarn --version)
if [ "${package_manager_version:0:1}" -gt 1 ] && [ "$workspace_error_message" = "Workspaces can only be enabled in private projects." ]
then
# From yarn 2 there's no way to output information about repo's workspaces
# in pure json. Here's how the output of `yarn workspaces list --json` looks like
# (stringified json objects separated with new lines):
#
# ```
# {"location":".","name":"the-monorepo"}
# {"location":"packages/blog-1","name":"dev blog"}
# {"location":"packages/blog-2","name":"personal blog"}
# ```
workspace_output=[$(paste -s -d ',' <(yarn workspaces list --json 2>/dev/null))]
workspace_exit_code=$?

if [ $workspace_exit_code -eq 0 ]
then
mapfile -t package_locations <<<"$(jq -r '.[].location | select(. != ".")' <<<"$workspace_output")"
fi
fi
fi
fi

if [ ${#package_locations[@]} -ne 0 ]
then
echo "$installer workspaces detected"
# Extract all the packages and respective locations.
restore_js_workspaces_cache "${package_locations[@]}"
else
echo "No $installer workspaces detected"
Expand Down

0 comments on commit 536caf3

Please sign in to comment.