-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (119 loc) · 5.56 KB
/
upgrade-to-next-internal-react.yml
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
name: Upgrade to Next.js internal React package version
on: push
jobs:
upgrade-to-next-internal-react:
name: Upgrade to Next.js internal React package version
runs-on: ubuntu-latest
# Skip workflow run if last committer was not Renovate Bot
if: github.event.head_commit.author.name == 'renovate[bot]'
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: pnpm/action-setup@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 'lts/*'
cache: 'pnpm'
- run: pnpm install
- name: Upgrade to Next.js internal React package version
run: |
set -e
matching_pattern="19.0.0-rc-.*-.*"
# Find all package.json files, omitting node_modules directories
packages=$(find . -name 'package.json' -not -path '*/node_modules/*')
# Check React version in Next.js, if installed
if [ -f "node_modules/next/package.json" ]; then
# Extract the React version from peerDependencies
react_peer_dependency=$(jq -r '.peerDependencies.react // empty' node_modules/next/package.json)
if [[ -z "$react_peer_dependency" ]]; then
echo "React peerDependency not found in Next.js package.json"
exit 0
fi
# Extract version after '||' and trim spaces
next_react_version=$(echo "$react_peer_dependency" | awk -F '\\|\\|' '{gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2}')
if [[ "$next_react_version" =~ $matching_pattern ]]; then
echo "React version in Next.js matches: $next_react_version"
else
echo "React version in Next.js does not match, aborting."
exit 0
fi
else
echo "Next.js not installed, skipping Next.js React version check."
exit 0
fi
# For each package.json, check if react/react-dom versions match the pattern, then update only the ones that do
declare -A lockfiles
for package in $packages; do
dir=$(dirname "$package")
# Initialize the jq script dynamically based on matching dependencies
jq_script='.'
# Check React versions in package.json
version_react=$(jq -r '.dependencies.react // empty' "$package" || true)
version_react_dom=$(jq -r '.dependencies["react-dom"] // empty' "$package" || true)
version_dev_react=$(jq -r '.devDependencies.react // empty' "$package" || true)
version_dev_react_dom=$(jq -r '.devDependencies["react-dom"] // empty' "$package" || true)
# Update only the versions that match the pattern
if [[ "$version_react" =~ $matching_pattern ]]; then
jq_script="$jq_script | .dependencies.react = \$react_version"
fi
if [[ "$version_react_dom" =~ $matching_pattern ]]; then
jq_script="$jq_script | .dependencies[\"react-dom\"] = \$react_version"
fi
if [[ "$version_dev_react" =~ $matching_pattern ]]; then
jq_script="$jq_script | .devDependencies.react = \$react_version"
fi
if [[ "$version_dev_react_dom" =~ $matching_pattern ]]; then
jq_script="$jq_script | .devDependencies[\"react-dom\"] = \$react_version"
fi
# Update the package.json if any dependency matched the pattern
if [ "$jq_script" != '.' ]; then
echo "Updating $package"
jq --arg react_version "$next_react_version" "$jq_script" "$package" > tmp.$$.json && mv tmp.$$.json "$package"
else
echo "No matching React versions in $package, skipping update."
fi
# Run package manager install if package.json was updated or if it is in the root directory
if [ "$jq_script" != '.' ] || [ "$dir" = "." ]; then
if [ -f "$dir/package-lock.json" ]; then
lockfiles["$dir"]="npm"
elif [ -f "$dir/yarn.lock" ]; then
lockfiles["$dir"]="yarn"
elif [ -f "$dir/pnpm-lock.yaml" ]; then
lockfiles["$dir"]="pnpm"
fi
fi
done
if [ ${#lockfiles[@]} -eq 0 ]; then
echo "No packages updated, exiting."
exit 0
fi
# Install dependencies in all locations where package.json was updated
for dir in "${!lockfiles[@]}"; do
manager=${lockfiles[$dir]}
echo "Running $manager install in $dir"
case "$manager" in
npm)
(cd "$dir" && npm install)
;;
yarn)
(cd "$dir" && yarn install)
;;
pnpm)
(cd "$dir" && pnpm install --no-frozen-lockfile)
;;
esac
done
# Commit changes, if any
git config user.email "github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
git add .
if git diff-index --quiet HEAD --; then
echo "No changes to commit, exiting."
exit 0
fi
# Use the React version from Next.js for the commit message
git commit -m "Upgrade React packages to $next_react_version"
# Push changes using OAuth2 token
git push https://oauth2:${{ secrets.UPGRADE_TO_NEXT_INTERNAL_REACT_GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }}