Created
October 31, 2024 14:10
-
-
Save jasonbekolay/5f7ba19f4d2be8bcdfdd31c1395c1e72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Usage: linkpackage <path> | |
# Will update package.json in the current directory to link to the package at <path> | |
# Intended to replicate the experience of running `yarn link <package>` | |
LINKED_PACKAGE_PATH=$1 | |
if [ ! -d "$LINKED_PACKAGE_PATH" ]; then | |
echo "$LINKED_PACKAGE_PATH is not a directory" | |
exit 1 | |
fi | |
LINKED_PACKAGE_PATH=$(realpath "$LINKED_PACKAGE_PATH") | |
LINKED_PACKAGE_JSON="$LINKED_PACKAGE_PATH/package.json" | |
if [ ! -f "$LINKED_PACKAGE_JSON" ]; then | |
echo "$PACKAGE_PATH does not contain a package.json file" | |
exit 1 | |
fi | |
if ! LINKED_PACKAGE_NAME=$(jq -r -e '.name' "$LINKED_PACKAGE_JSON"); then | |
echo "Could not read package name from $LINKED_PACKAGE_JSON" | |
exit 1 | |
fi | |
TARGET_PACKAGE_JSON="./package.json" | |
WORKING_PACKAGE_JSON="./package.json.tmp" | |
if jq -e '.dependencies."'"$LINKED_PACKAGE_NAME"'"' < $TARGET_PACKAGE_JSON > /dev/null ; then | |
jq '.dependencies."'"$LINKED_PACKAGE_NAME"'" = "link:'"$LINKED_PACKAGE_PATH"'"' < "$TARGET_PACKAGE_JSON" > "$WORKING_PACKAGE_JSON" | |
mv "$WORKING_PACKAGE_JSON" "$TARGET_PACKAGE_JSON" | |
echo "Linked $LINKED_PACKAGE_NAME in dependencies" | |
PACKAGE_JSON_UPDATED=true | |
fi | |
if jq -e '.devDependencies."'"$LINKED_PACKAGE_NAME"'"' < $TARGET_PACKAGE_JSON > /dev/null ; then | |
jq '.devDependencies."'"$LINKED_PACKAGE_NAME"'" = "link:'"$LINKED_PACKAGE_PATH"'"' < "$TARGET_PACKAGE_JSON" > "$WORKING_PACKAGE_JSON" | |
mv "$WORKING_PACKAGE_JSON" "$TARGET_PACKAGE_JSON" | |
echo "Linked $LINKED_PACKAGE_NAME in devDependencies" | |
PACKAGE_JSON_UPDATED=true | |
fi | |
if [ "$PACKAGE_JSON_UPDATED" = true ]; then | |
echo "Please run pnpm install to use the linked package" | |
else | |
echo "No changes made to $TARGET_PACKAGE_JSON" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment