Skip to content

Instantly share code, notes, and snippets.

@aklump
Last active April 24, 2026 18:44
Show Gist options
  • Select an option

  • Save aklump/9f8ca7a03490b62eb1f59802318d1ca2 to your computer and use it in GitHub Desktop.

Select an option

Save aklump/9f8ca7a03490b62eb1f59802318d1ca2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# @file
# Handle NPM dependencies in a directory and/or subdirectories
#
# ========= Bootstrap =========
s="${BASH_SOURCE[0]}";[[ "$s" ]] || s="${(%):-%N}";while [ -h "$s" ];do d="$(cd -P "$(dirname "$s")" && pwd)";s="$(readlink "$s")";[[ $s != /* ]] && s="$d/$s";done;__DIR__=$(cd -P "$(dirname "$s")" && pwd)
basepath="$(cd -P "$__DIR__/.." && pwd)"
if [[ -z "$APP_ROOT" ]]; then
APP_ROOT="$basepath"
fi
export APP_ROOT
# ========= Setup functions =========
##
# Echo the current directory relative to env APP_ROOT
#
# @echo The (relative) directory of the CWD
# @return 0
##
function echo_location() {
local relative base
relative="$(pwd -P)"
base="${APP_ROOT%/}"
if [[ "$relative" == "$base" ]]; then
relative="."
elif [[ "$relative" == "$base"/* ]]; then
relative="./${relative#$base/}"
fi
# Keep this for visual spacing.
echo
echo "$relative"
}
##
# Checks if the current working directory has browserlist in package.json
#
# @return 0 if it does
##
function has_browserslist() {
[[ -f "package.json" ]] && grep -q '"browserslist"' "package.json" && return 0
[[ -f ".browserslistrc" ]] && return 0
return 1
}
##
# Run the update process in a given extension directory
#
# @param string The directory path (module, component, theme) containing a package.json file.
#
# @echo The output of the process
# @return 1 If it fails for any reason
##
function update_deps_in() {
local path="$1"
(
cd "$path" || exit 1
if has_browserslist; then
echo_location && npx browserslist@latest --update-db || exit 1
fi
echo_location
yarn upgrade || exit 1
)
}
# ========= Target the components directory in this extension =========
for yarnlock in "$basepath"/components/*/yarn.lock; do
[ -e "$yarnlock" ] || continue
update_deps_in "$(dirname "$yarnlock")" || exit 1
done
# ========= Target this extension itself =========
update_deps_in "$basepath" || exit 1
# ========= Target a specific subdirectory =========
update_deps_in "$basepath/web/themes/custom/my_theme" || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment