Skip to content

Commit

Permalink
Improve efficiency of yarn-install's registry overrride step (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascrazy authored Jan 15, 2025
1 parent 05826b1 commit c8e402a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-pets-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"davinci-github-actions": patch
---

Improve efficiency of yarn-install's registry overrride step
48 changes: 25 additions & 23 deletions yarn-install/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,31 @@ runs:
echo "Snapshot original yarn.lock file to restore it later"
cp yarn.lock yarn.lock.original
echo "Extracting @toptal and @topkit entries"
touch yarn.lock.toptal
if grep -q '/@toptal\|/@topkit' yarn.lock; then
grep '/@toptal\|/@topkit' yarn.lock | awk '{print $2 " " $2}' > yarn.lock.toptal
fi
echo "Changing the URLs to the new registry for @toptal and @topkit entries, creating a TO/FROM list to be used when reverting back the URLs to the original registry"
sed -i -e "s#https://registry.yarnpkg.com/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#" yarn.lock.toptal
sed -i -e "s#https://registry.npmjs.org/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#" yarn.lock.toptal
echo "Changing the URLs to AR registry for all occurrences"
sed -i -e "s#https://registry.yarnpkg.com/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#g" yarn.lock
sed -i -e "s#https://registry.npmjs.org/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#g" yarn.lock
echo "Removing double quotes from the URLs"
sed -i -e "s/\"//g" yarn.lock.toptal
echo "Reverting the @toptal and @topkit packages to the original registry, working on revert fewer ocurrences (specific list) is faster than loop all the file"
while read -r line; do
url1="$(awk '{ print $1 }' <<<"$line")"
url2="$(awk '{ print $2 }' <<<"$line")"
sed -i -e "s~${url1}~${url2}~" yarn.lock
done < yarn.lock.toptal
PUBLIC_YARN_REGISTRY="https://registry.yarnpkg.com/"
PUBLIC_NPM_REGISTRY="https://registry.npmjs.org/"
TOPTAL_NPM_REGISTRY="https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/"
echo "Updating registry URLs in yarn.lock file and writing output to yarn.lock.temp"
# NOTE: setting IFS= (empty) for `read -r line` ensures that:
# 1. Leading and trailing whitespace in each line is preserved
# 2. The line is read exactly as-is, without any field splitting
# 3. Any whitespace characters within the line remain untouched
while IFS= read -r line; do
if [[ $line =~ /@toptal|/@topkit ]]; then
# For @toptal and @topkit packages, keep the original registry
echo "$line" >> yarn.lock.temp
else
# For all other packages, replace with new registry
modified_line="${line//$PUBLIC_YARN_REGISTRY/$TOPTAL_NPM_REGISTRY}"
modified_line="${modified_line//$PUBLIC_NPM_REGISTRY/$TOPTAL_NPM_REGISTRY}"
echo "$modified_line" >> yarn.lock.temp
fi
done < yarn.lock
echo "Replacing yarn.lock file with yarn.lock.temp"
mv yarn.lock.temp yarn.lock
echo "Registry URLs updated while preserving @toptal and @topkit package sources"
- name: Capture yarn.lock file checksum
id: yarn-lock-checksum
Expand Down

0 comments on commit c8e402a

Please sign in to comment.