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

Commit

Permalink
fix(ws-cache): using a tmp dir for workspace locations file & better …
Browse files Browse the repository at this point in the history
…handling of node_modules cache move out
  • Loading branch information
JGAntunes committed Feb 22, 2021
1 parent 620c1ce commit 4a61af7
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions run-build-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ mkdir -p $NETLIFY_CACHE_DIR/.gimme_cache/gocache
mkdir -p $NETLIFY_CACHE_DIR/.homebrew-cache
mkdir -p $NETLIFY_CACHE_DIR/.cargo


# tmp dir
NETLIFY_TMP_DIR=$(mktemp -d)

: ${YARN_FLAGS=""}
: ${NPM_FLAGS=""}
: ${BUNDLER_FLAGS=""}
Expand Down Expand Up @@ -106,7 +110,8 @@ run_yarn() {
if [ "$NETLIFY_YARN_WORKSPACES" = "true" ]
then
echo "NETLIFY_YARN_WORKSPACES feature flag set"
# Ignore path env var required in order to support yarn 2 projects
# 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 alwasy yarn 1.x). See https://yarnpkg.com/configuration/yarnrc#ignorePath
if YARN_IGNORE_PATH=1 yarn workspaces info
then
# Extract all the packages and respective locations. .data will be a JSON object like
Expand Down Expand Up @@ -697,14 +702,17 @@ install_dependencies() {
#
cache_artifacts() {
echo "Caching artifacts"
if [ "$NETLIFY_YARN_WORKSPACES" == "true" ] && [ -f package.json ]
then
cache_js_workspaces
fi

cache_cwd_directory ".bundle" "ruby gems"
cache_cwd_directory "bower_components" "bower components"
cache_cwd_directory "node_modules" "node modules"

if [ "$NETLIFY_YARN_WORKSPACES" == "true" ]
then
cache_node_modules
else
cache_cwd_directory "node_modules" "node modules"
fi

cache_cwd_directory ".venv" "python virtualenv"
cache_cwd_directory ".build" "swift build"
cache_cwd_directory ".netlify/plugins" "build plugins"
Expand Down Expand Up @@ -829,7 +837,20 @@ restore_js_workspaces_cache() {
# Retrieve hoisted node_modules
move_cache "$cache_dir/node_modules" "$NETLIFY_REPO_DIR/node_modules" "restoring workspace root node modules"
# Keep a record of the workspaces in the project in order to cache them later
echo "${locations[@]}" > "$cache_dir/.workspace_locations"
echo "${locations[@]}" > "$NETLIFY_TMP_DIR/.workspace_locations"
}

#
# Caches node_modules dirs for a js project. Either detects the presence of js workspaces
# via the `.workspace_locations` file, or looks at the node_modules in the cwd.
#
cache_node_modules() {
if [ -f "$NETLIFY_TMP_DIR/.workspace_locations" ]
then
cache_js_workspaces
else
cache_cwd_directory "node_modules" "node modules"
fi
}

#
Expand All @@ -838,16 +859,13 @@ restore_js_workspaces_cache() {
#
cache_js_workspaces() {
local cache_dir="$NETLIFY_CACHE_DIR/js-workspaces"
if [ -f "$cache_dir/.workspace_locations" ]
then
local locations=($(cat "$cache_dir/.workspace_locations"))
for location in "${locations[@]}"; do
mkdir -p "$cache_dir/$location"
move_cache "$NETLIFY_REPO_DIR/$location/node_modules" "$cache_dir/$location/node_modules" "saving workspace $location node modules"
done
# Retrieve hoisted node_modules
move_cache "$NETLIFY_REPO_DIR/node_modules" "$cache_dir/node_modules" "saving workspace root node modules"
fi
local locations=($(cat "$NETLIFY_TMP_DIR/.workspace_locations"))
for location in "${locations[@]}"; do
mkdir -p "$cache_dir/$location"
move_cache "$NETLIFY_REPO_DIR/$location/node_modules" "$cache_dir/$location/node_modules" "saving workspace $location node modules"
done
# Retrieve hoisted node_modules
move_cache "$NETLIFY_REPO_DIR/node_modules" "$cache_dir/node_modules" "saving workspace root node modules"
}

cache_cwd_directory() {
Expand Down

0 comments on commit 4a61af7

Please sign in to comment.