Skip to content

Instantly share code, notes, and snippets.

@bgotink
Last active January 11, 2021 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgotink/12bd885f4ca0cf9cfdab9ca789e4c840 to your computer and use it in GitHub Desktop.
Save bgotink/12bd885f4ca0cf9cfdab9ca789e4c840 to your computer and use it in GitHub Desktop.
Add binaries installed via yarn to the $PATH
function __update_yarn_bin --on-variable PWD
if ! set -qg FISH_YARN_BIN
set -g FISH_YARN_BIN (mktemp -d -t fish-yarn-bin-XXX)
set PATH $FISH_YARN_BIN $PATH
end
set -l bins (begin yarn bin --json; or true; end | jq -r .name)
for file in $bins
if ! test -f $FISH_YARN_BIN/$file -o $file = null
echo -e "#!/usr/bin/env bash\nexec yarn run -B $file \"\$@\"" >$FISH_YARN_BIN/$file
chmod +x $FISH_YARN_BIN/$file
end
end
for file in $FISH_YARN_BIN/*
if ! contains (basename $file) $bins
rm $file
end
end
end
__update_yarn_bin

Add this script to your fish config.

It sets up a listener. Every time the working directory changes, it checks what binaries are installed and accessible via yarn 2, and makes those accessible on the PATH.

In other words, if you've got a yarn 2 workspace you can run

yarn add -D typescript
tsc --init

instead of

yarn add -D typescript
yarn tsc --init

These binaries are available in your shell but also in other processes, making it possible to e.g. run

git ls-files | xargs prettier --write
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment