Skip to content

Instantly share code, notes, and snippets.

@cherryramatisdev
Last active December 14, 2023 17:44
Show Gist options
  • Save cherryramatisdev/5be1f6d6b081df79c0e8632cfffa375c to your computer and use it in GitHub Desktop.
Save cherryramatisdev/5be1f6d6b081df79c0e8632cfffa375c to your computer and use it in GitHub Desktop.
Script to automagically detect the different node package managers
#!/usr/bin/env perl
use v5.14;
use warnings;
use strict;
my $dir = `git rev-parse --show-toplevel`;
$dir =~ s/^\s+|\s+$//g;
chdir($dir) or die "Could not change to the directory $dir: $!";
sub get_package_manager {
if (-f "yarn.lock") {
return "yarn"
}
if (-f "package-lock.json") {
return "npm"
}
if (-f "pnpm-lock.yaml") {
return "pnpm"
}
if (-f "bun.lockb") {
return "bun"
}
return "none"
}
my $package_manager = get_package_manager;
if ($package_manager eq "none") {
say "Could not determine package manager";
exit 1;
}
my @actions = `jq -r '.scripts | keys[]' package.json`;
if ($ENV{COMP_LINE}) {
map {/^$ARGV[1]/ and say $_} @actions;
exit 0;
}
system("$package_manager @ARGV");
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment