Skip to content

Instantly share code, notes, and snippets.

@piousdeer
Last active March 8, 2021 03:59
Show Gist options
  • Save piousdeer/fb518220600060151e298fa891deeafb to your computer and use it in GitHub Desktop.
Save piousdeer/fb518220600060151e298fa891deeafb to your computer and use it in GitHub Desktop.
__npm_safeguard() {
python3 ~/npm_safeguard.py $1 && command "$@"
}
npm() {
__npm_safeguard npm "$@"
}
pnpm() {
__npm_safeguard pnpm "$@"
}
yarn() {
__npm_safeguard yarn "$@"
}

If you're anything like me, you've likely found yourself to have package-lock.json and yarn.lock and/or even pnpm-lock.yaml in the same project at least once. This script warns you when you attempt to use a package manager different from the one already used in the project.

Merge the provided .bashrc with yours and put the Python script into your home directory. If you're running Windows, make sure to use Git Bash or something.

Screenshot

import os
import sys
PACKAGE_MANAGERS = (
('npm', 'package-lock.json'),
('pnpm', 'pnpm-lock.yaml'),
('yarn', 'yarn.lock')
)
user_pm = sys.argv[1]
for pm, lockfile in PACKAGE_MANAGERS:
if pm != user_pm and os.path.exists(lockfile):
answer = input(f'Found {pm} lockfile. Continue? [y/n] ').lower()
if answer != 'y' and answer != 'yes':
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment