| 1 | package Mojo::Alien::npm; | ||||||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use File::chdir; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Mojo::JSON qw(decode_json false); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use constant DEBUG => ($ENV{MOJO_NPM_DEBUG} || $ENV{MOJO_WEBPACK_DEBUG}) && 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.5
Condition40%2/5
($ENV{'MOJO_NPM_DEBUG'} || $ENV{'MOJO_WEBPACK_DEBUG'}) && 1
$ENV{"MOJO_NPM_DEBUG"} or $ENV{"MOJO_WEBPACK_DEBUG"}
MC/DC0%0/2
($ENV{'MOJO_NPM_DEBUG'} || $ENV{'MOJO_WEBPACK_DEBUG'}) && 1
$ENV{"MOJO_NPM_DEBUG"}
$ENV{"MOJO_WEBPACK_DEBUG"}
Truth table33%1/3
A or B
A= $ENV{"MOJO_NPM_DEBUG"}B= $ENV{"MOJO_WEBPACK_DEBUG"}
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | has binary => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 12 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | return $ENV{MOJO_NPM_BINARY} ? $ENV{MOJO_NPM_BINARY} : 'npm'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 14 | 10 | }; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | has config => sub { path->to_abs->child('package.json') }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | has mode => sub { $ENV{NODE_ENV} || 'development' }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub dependencies { | ||||||||||||||||||||||||||||||
|
Subroutine
dependencies: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 20 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | croak "Can't get dependency info without package.json" unless -r $self->config; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my @args = $self->binary eq 'pnpm' ? qw(ls --json --silent) : qw(ls --json --parseable --silent); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 24 | 0 | my $dependencies; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | eval { | ||||||||||||||||||||||||||||||||
| 27 | 0 | my $NPM = $self->_run(@args); | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # "WARN" might come from pnpm, and it also returns an array-ref | ||||||||||||||||||||||||||||||||
| 30 | 0 | $dependencies = decode_json(join '', grep { !/WARN/ } <$NPM>); | |||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $dependencies = $dependencies->[0] if ref $dependencies eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $dependencies = {map { %{$dependencies->{$_} || {}} } qw(devDependencies dependencies)}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$dependencies->{$_} || {}
MC/DC0%0/1
$dependencies->{$_} || {}
$dependencies->{$_}
Truth table0%0/2
A
A= $dependencies->{$_}
| |||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 0 | croak sprintf '%s failed: %s', join(' ', $self->binary, @args), $@; | |||||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 0 | my $package = decode_json $self->config->slurp; | |||||||||||||||||||||||||||||||
| 38 | 0 | my %types = (devDependencies => 'dev', dependencies => 'prod', optionalDependencies => 'optional'); | |||||||||||||||||||||||||||||||
| 39 | 0 | for my $type (qw(optionalDependencies devDependencies dependencies)) { | |||||||||||||||||||||||||||||||
| 40 | 0 | for my $name (keys %{$package->{$type}}) { | |||||||||||||||||||||||||||||||
| 41 | 0 | $dependencies->{$name}{required} = $package->{$type}{$name}; | |||||||||||||||||||||||||||||||
| 42 | 0 | $dependencies->{$name}{type} = $types{$type}; | |||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $dependencies->{$name}{version} //= ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$dependencies->{$name}{"version"} //= ""
MC/DC0%0/1
$dependencies->{$name}{"version"} //= ""
$dependencies->{$name}{"version"}
Truth table0%0/2
A
A= $dependencies->{$name}{"version"}
| |||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 0 | return $dependencies; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | return $self if -r $self->config; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $self->_run($self->binary eq 'pnpm' ? qw(init) : qw(init -y)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | croak "$self->{basename} init failed: @{[$self->config]} was not generated." unless -r $self->config; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub install { | ||||||||||||||||||||||||||||||
|
Subroutine
install: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 59 | 0 | my ($self, $name, $info) = @_; | |||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | croak "Can't install packages without package.json" unless -w $self->config; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # Make sure npm can install devDependencies and dependency | ||||||||||||||||||||||||||||||||
| 63 | 0 | local $self->{mode} = ''; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | # Install everything | ||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | do { $self->_run('install'); return $self } unless $name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | # Install specific package | ||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $name = sprintf '%s@%s', $name, $info->{version} if $info->{version}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my $type = sprintf '--save-%s', $info->{type} || 'dev'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$info->{'type'} || 'dev'
MC/DC0%0/1
$info->{'type'} || 'dev'
$info->{'type'}
Truth table0%0/2
A
A= $info->{'type'}
| |||||||||||||||||||||||||||||||||
| 71 | 0 | $self->_run('install', $name, $type); | |||||||||||||||||||||||||||||||
| 72 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub _run { | ||||||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 76 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 77 | 0 | my @cmd = ($self->binary, @_); | |||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $self->{basename} ||= path($cmd[0])->basename; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"basename"} ||= path($cmd[0])->basename
MC/DC0%0/2
$self->{"basename"} ||= path($cmd[0])->basename
$self->{"basename"}
path($cmd[0])->basename
Truth table0%0/3
A ||= B
A= $self->{"basename"}B= path($cmd[0])->basename
| |||||||||||||||||||||||||||||||||
| 79 | 0 | local $CWD = $self->config->dirname->to_string; | |||||||||||||||||||||||||||||||
| 80 | 0 | local $ENV{NODE_ENV} = $self->mode; | |||||||||||||||||||||||||||||||
| 81 | 0 | warn "[NPM] cd $CWD && @cmd\n" if DEBUG; | |||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | open my $NPM, '-|', @cmd or die "Can't fork @cmd: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return $NPM if defined wantarray; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 0 | map { DEBUG && print } <$NPM>; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | # This is a utility function for the unit tests | ||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub _setup_working_directory { | ||||||||||||||||||||||||||||||
|
Subroutine
_setup_working_directory: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 89 | 0 | my ($class, $dir) = @_; | |||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $remove_tree = $ENV{MOJO_NPM_CLEAN} ? 'remove_tree' : sub { }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | chdir(my $work_dir = path($dir ? $dir : ('local', path($0)->basename))->to_abs->tap($remove_tree)->make_path) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 92 | or die "Couldn't set up working directory: $!"; | ||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | symlink $work_dir->dirname->child('node_modules')->make_path, 'node_modules' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
-e 'node_modules' || (symlink($work_dir->dirname->child('node_modules')->make_path, 'node_modules') || warn("Couldn't set up shared node_modules: $!"))
symlink $work_dir->dirname->child("node_modules")->make_path, "node_modules" or warn "Couldn't set up shared node_modules: $!"
MC/DC0%0/2
-e 'node_modules' || (symlink($work_dir->dirname->child('node_modules')->make_path, 'node_modules') || warn("Couldn't set up shared node_modules: $!"))
-e 'node_modules'
symlink $work_dir->dirname->child("node_modules")->make_path, "node_modules"
Truth table0%0/3
A || B
A= -e 'node_modules'B= symlink $work_dir->dirname->child("node_modules")->make_path, "node_modules"
| |||||||||||||||||||||||||||||||||
| 94 | or warn "Couldn't set up shared node_modules: $!" | ||||||||||||||||||||||||||||||||
| 95 | unless -e 'node_modules'; | ||||||||||||||||||||||||||||||||
| 96 | 0 | return $work_dir; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 10 | 1; | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | =encoding utf8 | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | Mojo::Alien::npm - Runs the external nodejs program npm | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | use Mojo::Alien::npm; | ||||||||||||||||||||||||||||||||
| 110 | my $npm = Mojo::Alien::npm->new; | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | $npm->init; | ||||||||||||||||||||||||||||||||
| 113 | $npm->install; | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | L<Mojo::Alien::webpack> is a class for runnig the external nodejs program | ||||||||||||||||||||||||||||||||
| 118 | L<npm|https://npmjs.com/>. | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | =head2 binary | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | $array_ref = $npm->binary; | ||||||||||||||||||||||||||||||||
| 125 | $npm = $npm->binary(['npm']); | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | The path to the npm executable. Default is "npm" unless the C<MOJO_NPM_BINARY> | ||||||||||||||||||||||||||||||||
| 128 | environment variable has been set. This can also be set to "pnpm" in case you | ||||||||||||||||||||||||||||||||
| 129 | prefer L<https://pnpm.io/>. | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | =head2 config | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | $path = $npm->config; | ||||||||||||||||||||||||||||||||
| 134 | $npm = $npm->config(path->to_abs->child('package.json')); | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | Holds an I</absolute> path to "package.json". | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | =head2 mode | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | $str = $npm->mode; | ||||||||||||||||||||||||||||||||
| 141 | $npm = $npm->mode('development'); | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | Should be either "development" or "production". Will be used as "NODE_ENV" | ||||||||||||||||||||||||||||||||
| 144 | environment variable when calling "npm". | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | =head2 dependencies | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | $dependencies = $npm->dependencies; | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | Used to get dependencies from L</config> combined with information from | ||||||||||||||||||||||||||||||||
| 153 | C<npm ls>. The returned hash-ref looks like this: | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||||||
| 156 | "package-name" => { | ||||||||||||||||||||||||||||||||
| 157 | required => $str, # version from package.json | ||||||||||||||||||||||||||||||||
| 158 | type => $str, # dev, optional or prod | ||||||||||||||||||||||||||||||||
| 159 | version => $str, # installed version | ||||||||||||||||||||||||||||||||
| 160 | ... | ||||||||||||||||||||||||||||||||
| 161 | }, | ||||||||||||||||||||||||||||||||
| 162 | ... | ||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | =head2 init | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | $npm->init; | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | Used to create a default L</config> file. | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | =head2 install | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | $npm->install; | ||||||||||||||||||||||||||||||||
| 174 | $npm->install('package-name'); | ||||||||||||||||||||||||||||||||
| 175 | $npm->install('package-name', {type => 'prod', version => '0.1.2'}); | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | Installs either all modules from L</config> or a given package by name. An | ||||||||||||||||||||||||||||||||
| 178 | additional C<$info> hash can also be provided. | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | L<Mojolicious::Plugin::Webpack>. | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||||||