-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade-packages.sh
executable file
·100 lines (83 loc) · 2.16 KB
/
upgrade-packages.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
set -e
upgrade_packages() {
input="$@"
package_names=()
package_versions=()
regex='^(@?[a-z0-9\/\-]+)(@.+)?$'
for package in $input; do
package_name="$(echo "$package" | sed -r 's/'"$regex"'/\1/i')"
package_version="$(echo "$package" | sed -r 's/'"$regex"'/\2/i')"
package_names+=("$package_name")
package_versions+=("$package_name${package_version:-@latest}")
done
echo "Upgrading to $package_versions"
select_filter=".\"${package_names[0]}\" != null"
for package_name in ${package_names[@]:1}; do
select_filter="$select_filter and .\"$package_name\" != null"
done
all_packages="$(jq --raw-output \
'[.workspaces[]]
| map("\(.)/package.json")
| .[]' \
package.json)"
relevant_workspaces="$(jq --raw-output \
'.
# join all dependency sets
| (.dependencies // {})
* (.devDependencies // {})
* (.peerDependencies // {})
* (.optionalDependencies // {})
# filter for packages with the specified packages
| select('"$select_filter"')
# get name of file
| input_filename
# get workspace name
| split("/package.json")
| .[0]
# convert to npm workspace arg
| "--workspace=\(.)"' \
$all_packages)"
echo "$relevant_workspaces"
if [ -z "$relevant_workspaces" ]; then
echo "No workspaces with specified packages found"
exit 0
fi
npm install \
${package_versions[*]} \
$relevant_workspaces
}
help () {
echo -e ""
echo -e "Usage:\t$(basename $0) [options] PACKAGE[@VERSION] [PACKAGE[@VERSION]...]"
echo -e ""
echo -e "Options:"
echo -e " -h, --help Show this message"
echo -e ""
echo -e "Upgrades all eslint versions together."
}
option_error () {
echo "Unknown argument '$1'"
help
exit 128
}
main () {
while getopts 'h-:' opt; do
case "$opt" in
h) help; exit 0;;
-)
case "${OPTARG}" in
help) help; exit 0;;
*) option_error "$OPTARG";;
esac
;;
*) option_error "$OPTARG";;
esac
done
if [ -z "$1" ]; then
echo "A package name is required"
exit 1
fi
upgrade_packages "$@"
}
main "$@"