From d985be18205fd644183e2d5f3f983f2b68a319cb Mon Sep 17 00:00:00 2001 From: Zackery Griesinger Date: Wed, 16 Mar 2022 10:04:39 -0500 Subject: [PATCH] feat: Support npm workspaces caching (#752) --- run-build-functions.sh | 66 ++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/run-build-functions.sh b/run-build-functions.sh index 43603fdd..e642d1e8 100755 --- a/run-build-functions.sh +++ b/run-build-functions.sh @@ -86,40 +86,18 @@ install_deps() { fi } -run_yarn() { - yarn_version=$1 - if [ -d $NETLIFY_CACHE_DIR/yarn ] - then - export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH - fi - restore_home_cache ".yarn_cache" "yarn cache" - - if [ $(which yarn) ] && [ "$(yarn --version)" != "$yarn_version" ] - then - echo "Found yarn version ($(yarn --version)) that doesn't match expected ($yarn_version)" - rm -rf $NETLIFY_CACHE_DIR/yarn $HOME/.yarn - npm uninstall yarn -g - fi - - if ! [ $(which yarn) ] - then - echo "Installing yarn at version $yarn_version" - rm -rf $HOME/.yarn - bash /usr/local/bin/yarn-installer.sh --version $yarn_version - mv $HOME/.yarn $NETLIFY_CACHE_DIR/yarn - export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH - fi - - +restore_node_modules() { local workspace_output local workspace_exit_code + 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_exit_code=$? if [ $workspace_exit_code -eq 0 ] then - echo "Yarn workspaces detected" + echo "$installer workspaces detected" local package_locations # Extract all the packages and respective locations. .data will be a JSON object like # { @@ -134,9 +112,36 @@ run_yarn() { mapfile -t package_locations <<< "$(echo "$workspace_output" | jq -r '.data | fromjson | to_entries | .[].value.location')" restore_js_workspaces_cache "${package_locations[@]}" else - echo "No yarn workspaces detected" + echo "No $installer workspaces detected" restore_cwd_cache node_modules "node modules" fi +} + +run_yarn() { + yarn_version=$1 + if [ -d $NETLIFY_CACHE_DIR/yarn ] + then + export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH + fi + restore_home_cache ".yarn_cache" "yarn cache" + + if [ $(which yarn) ] && [ "$(yarn --version)" != "$yarn_version" ] + then + echo "Found yarn version ($(yarn --version)) that doesn't match expected ($yarn_version)" + rm -rf $NETLIFY_CACHE_DIR/yarn $HOME/.yarn + npm uninstall yarn -g + fi + + if ! [ $(which yarn) ] + then + echo "Installing yarn at version $yarn_version" + rm -rf $HOME/.yarn + bash /usr/local/bin/yarn-installer.sh --version $yarn_version + mv $HOME/.yarn $NETLIFY_CACHE_DIR/yarn + export PATH=$NETLIFY_CACHE_DIR/yarn/bin:$PATH + fi + + restore_node_modules "yarn" echo "Installing NPM modules using Yarn version $(yarn --version)" run_npm_set_temp @@ -166,7 +171,12 @@ run_npm_set_temp() { } run_npm() { - restore_cwd_cache node_modules "node modules" + if [ -n "$EXPERIMENTAL_NPM_WORKSPACES_CACHING" ] + then + restore_node_modules "npm" + else + restore_cwd_cache node_modules "node modules" + fi if [ -n "$NPM_VERSION" ] then