Skip to content

Instantly share code, notes, and snippets.

@idrise
Created February 2, 2023 19:54
Show Gist options
  • Save idrise/66c85776d161d06c10dc8e37681e8e71 to your computer and use it in GitHub Desktop.
Save idrise/66c85776d161d06c10dc8e37681e8e71 to your computer and use it in GitHub Desktop.
function check_package_manager() {
current_dir=$(pwd)
while [ "$(pwd)" != "/" ]; do
if [ -f "pnpm-lock.yaml" ]; then
if [ "${1}" != "pnpm" ]; then
echo "Looks like you're using the wrong package manager, partner!"
echo "It's pnpm time, cowboy."
cd "${current_dir}"
return 1
fi
break
elif [ -f "yarn.lock" ]; then
if [ "${1}" != "yarn" ]; then
echo "Uh oh, it looks like you're using the wrong package manager!"
echo "Time to lasso that dependency with Yarn."
cd "${current_dir}"
return 1
fi
break
elif [ -f "package-lock.json" ]; then
if [ "${1}" != "npm" ]; then
echo "Whoa there, it looks like you're using the wrong package manager!"
echo "Time to wrangle those dependencies with npm."
cd "${current_dir}"
return 1
fi
break
fi
cd ..
done
cd "${current_dir}"
}
alias npm="check_package_manager npm && npm"
alias yarn="check_package_manager yarn && yarn"
alias pnpm="check_package_manager pnpm && pnpm"
@mxdvl
Copy link

mxdvl commented Feb 3, 2023

Great, here’s the Fish equivalent:

function check_package_manager
  set current_dir $(pwd)
  set manager $argv[1]

  while test pwd != "/"
    if test -f "pnpm-lock.yaml"
      if test "$manager" != "pnpm";
        echo "Looks like you're using the wrong package manager, partner!"
        echo "It's pnpm time, cowboy."
        cd $current_dir
        return 1
      end
      break
    else if test -f "yarn.lock"
      if test "$manager" != "yarn"
        echo "Uh oh, it looks like you're using the wrong package manager!"
        echo "Time to lasso that dependency with Yarn."
        cd $current_dir
        return 1
      end
      break
    else if test -f "package-lock.json"
      if test "$manager" != "npm"
        echo "Whoa there, it looks like you're using the wrong package manager!"
        echo "Time to wrangle those dependencies with npm."
        cd $current_dir
        return 1
      end
      break
    end
    sleep 1
    cd ..
  end
  cd $current_dir
end

And then your individual functions aliases:

function pnpm --description 'alias pnpm check_package_manager pnpm && pnpm'
  check_package_manager pnpm && command pnpm $argv        
end

@idrise
Copy link
Author

idrise commented Feb 3, 2023

sometimes I fish but mostly I zsh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment