|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2019, imqueue.com <[email protected]> |
| 3 | +# |
| 4 | +# Permission to use, copy, modify, and/or distribute this software for any |
| 5 | +# purpose with or without fee is hereby granted, provided that the above |
| 6 | +# copyright notice and this permission notice appear in all copies. |
| 7 | +# |
| 8 | +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 9 | +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 10 | +# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 11 | +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 12 | +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 13 | +# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 14 | +# PERFORMANCE OF THIS SOFTWARE. |
| 15 | +argv=() |
| 16 | +services=() |
| 17 | +path="." |
| 18 | +cwd="$(pwd)" |
| 19 | +v_type="prerelease" |
| 20 | +do_commit=0 |
| 21 | +do_not_update=0 |
| 22 | + |
| 23 | +function in_services { |
| 24 | + local service_name="$1" |
| 25 | + local service_list_lookup=" ${services[*]} " |
| 26 | + local service_name_lookup=" ${service_name} " |
| 27 | + |
| 28 | + if [[ "$service_list_lookup" =~ $service_name_lookup ]]; then |
| 29 | + echo 1 |
| 30 | + else |
| 31 | + echo 0 |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +function usage { |
| 36 | + echo "Usage: $0 [-hcu] [-p path] [-s services] [-v type]" >&2 |
| 37 | + echo " Performs dependencies update on services located under given path" >&2 |
| 38 | + echo " Before running this command, make sure services are not in dirty git state" >&2 |
| 39 | + echo " [-h] - print this usage information" >&2 |
| 40 | + echo " [-v type] - set new version using one of typed: major|minor|path|prerelease. By default is prerelease" >&2 |
| 41 | + echo " [-c] - if specified it will try to do commit and push" >&2 |
| 42 | + echo " [-u] - if specified will do not update update deps, and performs other tasks only" >&2 |
| 43 | + echo " [-p path] - path to a directory with services repositories, by default is current directory" >&2 |
| 44 | + echo " [-s services] - comma-separated services list (repositories names), if not passed will scan path for a services presence" |
| 45 | +} |
| 46 | + |
| 47 | +# parse command-line args |
| 48 | +while [[ $# -gt 0 ]]; do |
| 49 | + unset OPTIND |
| 50 | + unset OPTARG |
| 51 | + |
| 52 | + while getopts hucvp:s: options; do |
| 53 | + case ${options} in |
| 54 | + p) path="$OPTARG" ;; |
| 55 | + c) do_commit=1 ;; |
| 56 | + u) do_not_update=1 ;; |
| 57 | + s) IFS=',' read -ra services <<< "$OPTARG" ;; |
| 58 | + v) v_type="${OPTARG}" ;; |
| 59 | + h) usage ; exit 0 ;; |
| 60 | + \?|*) usage ; exit 1 ;; |
| 61 | + esac |
| 62 | + done |
| 63 | + |
| 64 | + shift $((OPTIND-1)) |
| 65 | + argv+=("$1") |
| 66 | + shift |
| 67 | +done |
| 68 | + |
| 69 | +# load services from path if they were not provided by command-line option |
| 70 | +if [[ 0 -eq "${#services[@]}" ]]; then |
| 71 | + mapfile -t service_entries < <(find \ |
| 72 | + "$path"/*/src \ |
| 73 | + -type f \ |
| 74 | + -name "*.ts" \ |
| 75 | + -exec grep -lP 'extends\s+IMQ(Service|Client)\s*\{' {} +) |
| 76 | + |
| 77 | + for file in "${service_entries[@]}"; do |
| 78 | + IFS='/' read -ra file_path <<< "${file#${path}/}" |
| 79 | + service_name="${file_path[0]}" |
| 80 | + present=$(in_services "$service_name") |
| 81 | + |
| 82 | + if [[ 0 -eq ${present} ]]; then |
| 83 | + services+=( "$service_name" ) |
| 84 | + fi |
| 85 | + done |
| 86 | +fi |
| 87 | + |
| 88 | +if ! [[ -x "$(command -v npm-check-updates)" ]]; then |
| 89 | + npm i -g npm-check-updates || exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +if [[ do_commit -eq 0 && do_not_update -eq 1 ]]; then |
| 93 | + echo "Nothing to perform, suggest do update and/or commit." >&2 |
| 94 | + usage |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +for svc in "${services[@]}"; do |
| 99 | + dir="${path}/${svc}" |
| 100 | + |
| 101 | + cd ${dir} |
| 102 | + |
| 103 | + if [[ do_not_update -ne 1 ]]; then |
| 104 | + git pull || exit 1 |
| 105 | + ncu -u |
| 106 | + npm i |
| 107 | + fi |
| 108 | + |
| 109 | + if [[ do_commit -eq 1 ]]; then |
| 110 | + git commit -am "chore: dependencies update" |
| 111 | + |
| 112 | + case "${v_type}" in |
| 113 | + minor|major|patch|prerelease) ;; |
| 114 | + *) v_type="prerelease" |
| 115 | + esac |
| 116 | + |
| 117 | + npm version "$v_type" |
| 118 | + git push --follow-tags |
| 119 | + fi |
| 120 | + |
| 121 | + cd ${cwd} |
| 122 | +done |
0 commit comments