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

Commit d985be1

Browse files
authored
feat: Support npm workspaces caching (#752)
1 parent e8d0f81 commit d985be1

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

run-build-functions.sh

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,40 +86,18 @@ install_deps() {
8686
fi
8787
}
8888

89-
run_yarn() {
90-
yarn_version=$1
91-
if [ -d $NETLIFY_CACHE_DIR/yarn ]
92-
then
93-
export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH
94-
fi
95-
restore_home_cache ".yarn_cache" "yarn cache"
96-
97-
if [ $(which yarn) ] && [ "$(yarn --version)" != "$yarn_version" ]
98-
then
99-
echo "Found yarn version ($(yarn --version)) that doesn't match expected ($yarn_version)"
100-
rm -rf $NETLIFY_CACHE_DIR/yarn $HOME/.yarn
101-
npm uninstall yarn -g
102-
fi
103-
104-
if ! [ $(which yarn) ]
105-
then
106-
echo "Installing yarn at version $yarn_version"
107-
rm -rf $HOME/.yarn
108-
bash /usr/local/bin/yarn-installer.sh --version $yarn_version
109-
mv $HOME/.yarn $NETLIFY_CACHE_DIR/yarn
110-
export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH
111-
fi
112-
113-
89+
restore_node_modules() {
11490
local workspace_output
11591
local workspace_exit_code
92+
local installer=$1
11693
# YARN_IGNORE_PATH will ignore the presence of a local yarn executable (i.e. yarn 2) and default
11794
# to using the global one (which, for now, is always yarn 1.x). See https://yarnpkg.com/configuration/yarnrc#ignorePath
95+
# we can actually use this command for npm workspaces as well
11896
workspace_output="$(YARN_IGNORE_PATH=1 yarn workspaces --json info 2>/dev/null)"
11997
workspace_exit_code=$?
12098
if [ $workspace_exit_code -eq 0 ]
12199
then
122-
echo "Yarn workspaces detected"
100+
echo "$installer workspaces detected"
123101
local package_locations
124102
# Extract all the packages and respective locations. .data will be a JSON object like
125103
# {
@@ -134,9 +112,36 @@ run_yarn() {
134112
mapfile -t package_locations <<< "$(echo "$workspace_output" | jq -r '.data | fromjson | to_entries | .[].value.location')"
135113
restore_js_workspaces_cache "${package_locations[@]}"
136114
else
137-
echo "No yarn workspaces detected"
115+
echo "No $installer workspaces detected"
138116
restore_cwd_cache node_modules "node modules"
139117
fi
118+
}
119+
120+
run_yarn() {
121+
yarn_version=$1
122+
if [ -d $NETLIFY_CACHE_DIR/yarn ]
123+
then
124+
export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH
125+
fi
126+
restore_home_cache ".yarn_cache" "yarn cache"
127+
128+
if [ $(which yarn) ] && [ "$(yarn --version)" != "$yarn_version" ]
129+
then
130+
echo "Found yarn version ($(yarn --version)) that doesn't match expected ($yarn_version)"
131+
rm -rf $NETLIFY_CACHE_DIR/yarn $HOME/.yarn
132+
npm uninstall yarn -g
133+
fi
134+
135+
if ! [ $(which yarn) ]
136+
then
137+
echo "Installing yarn at version $yarn_version"
138+
rm -rf $HOME/.yarn
139+
bash /usr/local/bin/yarn-installer.sh --version $yarn_version
140+
mv $HOME/.yarn $NETLIFY_CACHE_DIR/yarn
141+
export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH
142+
fi
143+
144+
restore_node_modules "yarn"
140145

141146
echo "Installing NPM modules using Yarn version $(yarn --version)"
142147
run_npm_set_temp
@@ -166,7 +171,12 @@ run_npm_set_temp() {
166171
}
167172

168173
run_npm() {
169-
restore_cwd_cache node_modules "node modules"
174+
if [ -n "$EXPERIMENTAL_NPM_WORKSPACES_CACHING" ]
175+
then
176+
restore_node_modules "npm"
177+
else
178+
restore_cwd_cache node_modules "node modules"
179+
fi
170180

171181
if [ -n "$NPM_VERSION" ]
172182
then

0 commit comments

Comments
 (0)