Skip to content

Commit

Permalink
Merge pull request #7 from VincentHardouin/feat-add-step-by-step
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentHardouin authored Nov 6, 2022
2 parents c01ad2f + b981137 commit 346e909
Showing 1 changed file with 63 additions and 18 deletions.
81 changes: 63 additions & 18 deletions npm-bump
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare -a old_version_packages
declare -a new_version_packages
declare -a no_updated_packages
markdown=0
step_by_step=0

trap ctrl_c SIGINT

Expand Down Expand Up @@ -35,7 +36,7 @@ function handle_arguments() {
show_help
exit
;;
-m | --mardown)
-m | --markdown)
markdown=1
;;
-e | --exclude)
Expand All @@ -46,6 +47,9 @@ function handle_arguments() {
-nt | --no-test)
no_test=1
;;
-sbs | --step-by-step)
step_by_step=1
;;
*)
break
;;
Expand All @@ -60,6 +64,7 @@ function show_help() {
printf " %-25s %s\n" "-m, --markdown" "display updated packages in markdown table"
printf " %-25s %s\n" "-e, --exclude" "exclude package name seprated by a comma (e.g -e lodash,mocha)"
printf " %-25s %s\n" "-nt, --no-test" "does not run test command when update package"
printf " %-25s %s\n" "-sbs, --step-by-step" "test bump version step by step"
}

function commit_bump() {
Expand All @@ -77,6 +82,52 @@ function add_updated_package_informations() {
new_version_packages+=("$3")
}

function verlte() { printf '%s\n%s' "$1" "$2" | sort -C -V; }
function verlt() { ! verlte "$2" "$1"; }

function get_versions() {
read -r -d '\n' -a versions < <( npm view "$1" versions --json | awk -F "[,\n]" '!/\[|beta|alpha|rc|\]/ { gsub(/"/, "", $1); print $1}' | sort -V --reverse )
}

function filter_new_versions() {
for i in "${versions[@]}"
do
# shellcheck disable=SC2086
if verlt $1 $i; then
desc_newest_versions+=("$i")
else
break
fi
done
}

function get_newest_versions() {
get_versions "$1"
filter_new_versions "$2"
read -r -d '\n' -a newest_versions < <( printf '%s\n' "${desc_newest_versions[@]}" | sort -V )
}

function install_version() {
echo "Try to bump $1 from $2 to $3"

if [ -z ${no_test} ]; then
npm install "$1"@"$3" && npm test
else
npm install "$1"@"$3"
fi

# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
commit_bump "$1" "$2" "$3"
add_updated_package_informations "$1" "$2" "$3"
status=0
else
no_updated_packages+=("$1")
undo_changes
status=1
fi
}

function bump_version() {
if [ -z ${exclude+x} ]; then
outdated_packages=$(npm outdated | tail -n +2)
Expand All @@ -86,24 +137,18 @@ function bump_version() {

if [ -n "$outdated_packages" ]; then
# shellcheck disable=SC2034
while read -r package actual_version coll3 latest_version col4; do
echo "Try to bump ${package} from ${actual_version} to ${latest_version}"

if [ -z ${no_test} ]; then
npm install "$package"@"$latest_version" && npm test
else
npm install "$package"@"$latest_version"
fi

# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
commit_bump "$package" "$actual_version" "$latest_version"
add_updated_package_informations "$package" "$actual_version" "$latest_version"
while read -r package actual_version col3 latest_version col4; do
if [ ${step_by_step} -eq 0 ]; then
install_version "${package}" "${actual_version}" "${latest_version}"
else
no_updated_packages+=("${package}")
undo_changes
get_newest_versions "${package}" "${actual_version}"
for version in "${newest_versions[@]}"; do
install_version "${package}" "${actual_version}" "${version}"
if [ ${status} -eq 1 ]; then
break
fi
done
fi

done <<<"$outdated_packages"
fi
}
Expand Down Expand Up @@ -162,4 +207,4 @@ function show_result() {
handle_arguments "$@"
npm_files_exist
bump_version
show_result
show_result

0 comments on commit 346e909

Please sign in to comment.