27 August 2024
153 words
#!/usr/bin/env perl
# Node dependency manager. Never wonder whether to use `yarn` or `npm` again.
# Usage:
# np --> runs `yarn` or `npm i`
# np <args> --> runs `yarn <args>` or `npm run <args>`
use strict;
use warnings;
if (!-e 'package.json') {
my $did_run = 0;
my @dirs = grep { -d $_ } glob('*');
for my $dir (@dirs) {
$did_run = 1;
chdir($dir);
if (-e 'package.json') {
print "Running `np` in $dir\n";
my $exit_code = system('np ' . join(' ', @ARGV));
exit($exit_code >> 8) if $exit_code != 0;
}
chdir('..');
}
if ($did_run == 0) {
print "No package.json found. Exiting.\n";
exit(1);
}
exit(0);
}
my $exit_code;
if (-e 'package-lock.json') {
if (@ARGV) {
$exit_code = system('npm run ' . join(' ', @ARGV));
} else {
$exit_code = system('npm i');
}
} else {
$exit_code = system('yarn ' . join(' ', @ARGV));
}
exit($exit_code >> 8);