From 81ef87485182eec273b7bdc626a7c675f45fc6e0 Mon Sep 17 00:00:00 2001 From: Marshall Ku Date: Fri, 17 May 2024 17:33:26 +0900 Subject: [PATCH] Improve package updater to handle workspace packages --- scripts/update-packages.sh | 49 ++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/scripts/update-packages.sh b/scripts/update-packages.sh index d906e4d4..7ac10613 100644 --- a/scripts/update-packages.sh +++ b/scripts/update-packages.sh @@ -16,18 +16,57 @@ confirm() { done } +workspace=false + +parse_args() { + case "$1" in + -w | --workspace) + workspace=true + ;; + esac +} + +while [[ "$#" -ge 1 ]]; do + parse_args "$1" + shift 1 +done + # select a package in apps, packages -target=$(find apps/ packages/ -mindepth 1 -maxdepth 1 -type d | fzf) -package_name=$(grep '"name"' "${target}/package.json" | awk -F ': ' '{print $2}' | tr -d '",') +if [ "$workspace" = true ]; then + target='' +else + target=$(find apps/ packages/ -mindepth 1 -maxdepth 1 -type d | fzf) +fi -cd "$target" || exit 1 +if [ "$workspace" = false ] && [ -z "$target" ]; then + echo 'No target selected' + exit 0 +fi + +if [ "$workspace" = true ]; then + package_file='package.json' +else + package_file="${target}/package.json" +fi + +package_name=$(grep '"name"' "${package_file}" | awk -F ': ' '{print $2}' | tr -d '",') + +if [ -n "$target" ]; then + cd "$target" || exit 1 +fi packages=$(pnpm outdated) IFS=$'\n' mapfile -t lines <<<"$packages" regex="│ ([a-zA-Z0-9\._@\/-]+)(.*)? │ +([0-9\.]+) (\(wanted +([0-9\.]+)\))? +│ +([0-9\.]+) +│" -cmd='pnpm i ' +if [ "$workspace" = true ]; then + default_cmd='pnpm i -w ' +else + default_cmd='pnpm i ' +fi + +cmd="$default_cmd" for ((i = 0; i < ${#lines[@]}; i++)); do line=${lines[$i]} @@ -42,7 +81,7 @@ for ((i = 0; i < ${#lines[@]}; i++)); do fi done -if [[ "$cmd" == 'pnpm i ' ]]; then +if [[ "$cmd" == "$default_cmd" ]]; then echo 'Packages are up to date, installation step is skipped' exit 0 fi