-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupdate-peer-dependency.sh
executable file
·60 lines (49 loc) · 2.28 KB
/
update-peer-dependency.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
#!/bin/bash
# Update a specific peer dependency across all packages in the monorepo
# Usage example:
# in root folder execute:
# ./update-peer-dependency.sh <lib-name> <version>
# ./update-peer-dependency.sh rich-domain 1.25.0
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Please install jq to proceed."
exit 1
fi
# Check if the correct number of arguments is provided
# The script expects two arguments: the name of the dependency and the desired version.
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <dependency> <version>"
echo "Example: $0 rich-domain 1.25.0-beta"
exit 1
fi
# Assign the arguments to variables
DEPENDENCY=$1 # The name of the dependency to be updated
VERSION=$2 # The version to be set for the dependency
# Directory where the packages are located. The script will look inside this directory.
PACKAGES_DIR="./packages"
# Check if the /packages directory exists
if [ ! -d "$PACKAGES_DIR" ]; then
echo "The /packages directory does not exist! Please ensure the path is correct."
exit 1
fi
# Start the loop to iterate over all folders inside the /packages directory
for PACKAGE in $PACKAGES_DIR/*/; do
# Check if the current path is a directory
if [ -d "$PACKAGE" ]; then
# Build the path for the package.json file inside the package
PACKAGE_JSON="${PACKAGE}package.json"
# Check if the package.json file exists in the current directory
if [ -f "$PACKAGE_JSON" ]; then
# Check if the package.json contains the specific dependency under peerDependencies
if jq -e ".peerDependencies | has(\"$DEPENDENCY\")" "$PACKAGE_JSON" > /dev/null; then
# Update the version of the dependency in the package.json file
echo "Updating '$DEPENDENCY' to version ^$VERSION in $PACKAGE_JSON"
# Use jq to modify the version of the dependency
# The result is written to a temporary file and then replaces the original file
jq ".peerDependencies[\"$DEPENDENCY\"] = \"^$VERSION\"" "$PACKAGE_JSON" > "$PACKAGE_JSON.tmp" && mv "$PACKAGE_JSON.tmp" "$PACKAGE_JSON"
fi
fi
fi
done
# Message indicating the script has finished successfully
echo "Update completed."