Mojo/Alien/npm.pm

filter: Statementstmt 27.8% 22 / 79 Branchbran 0.0% 0 / 36 Conditioncond 10.5% 2 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 40.0% 6 / 15 Podpod 100% 3 / 3 totaltotal 20.5% 33 / 161 CC 28 SCAR 35.1
CC
28
Cov
18%
dependencies
90.0
_setup_working_directory
42.0
install
30.0
CRAP 33.6
« Mojo/Alien/webpack.pm ↑ Summary Mojo/Alien/rollup.pm »
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
!ll
100
$ENV{"MOJO_NPM_DEBUG"} or $ENV{"MOJO_WEBPACK_DEBUG"}
l!l&&r!l&&!r
0010
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"}
ABresult
000
1X1
011
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
BranchTrueFalseTotal
$ENV{'MOJO_NPM_BINARY'} ? : 0 0 0
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
BranchTrueFalseTotal
$ENV{"NODE_ENV"} or "development" 0 0 0
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
BranchTrueFalseTotal
unless -r $self->config 0 0 0
22  
23 0 ▶ my @args = $self->binary eq 'pnpm' ? qw(ls --json --silent) : qw(ls --json --parseable --silent);
Branch: 1 branch
BranchTrueFalseTotal
$self->binary eq 'pnpm' ? : 0 0 0
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
BranchTrueFalseTotal
if ref $dependencies eq "ARRAY" 0 0 0
32 0 ▶ $dependencies = {map { %{$dependencies->{$_} || {}} } qw(devDependencies dependencies)};
Condition0%0/2
$dependencies->{$_} || {}
l!l
00
MC/DC0%0/1
$dependencies->{$_} || {}
$dependencies->{$_}
Truth table0%0/2
A
A= $dependencies->{$_}
Aresult
11
00
33 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $NPM = $self->_run(@args); $dependencies = decode_json(join("", grep({not /WARN/u;} readline $NPM))); $dependencies = $dependencies->[0] if ref $dependencies eq "ARRAY"; $dependencies = {map({%{$dependencies->{$_} or {};};} "devDependencies", "dependencies")} } } or do { croak(sprintf('%s failed: %s', join(" ", $self->binary, @args), $@)) } 0 0 0
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"} //= ""
l!l
00
MC/DC0%0/1
$dependencies->{$name}{"version"} //= ""
$dependencies->{$name}{"version"}
Truth table0%0/2
A
A= $dependencies->{$name}{"version"}
Aresult
11
00
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
BranchTrueFalseTotal
if -r $self->config 0 0 0
53 0 ▶ $self->_run($self->binary eq 'pnpm' ? qw(init) : qw(init -y));
Branch: 1 branch
BranchTrueFalseTotal
$self->binary eq 'pnpm' ? : 0 0 0
54 0 ▶ croak "$self->{basename} init failed: @{[$self->config]} was not generated." unless -r $self->config;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $self->config 0 0 0
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
BranchTrueFalseTotal
unless -w $self->config 0 0 0
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
BranchTrueFalseTotal
unless ($name) 0 0 0
67  
68 # Install specific package
69 0 ▶ $name = sprintf '%s@%s', $name, $info->{version} if $info->{version};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"version"} 0 0 0
70 0 ▶ my $type = sprintf '--save-%s', $info->{type} || 'dev';
Condition0%0/2
$info->{'type'} || 'dev'
l!l
00
MC/DC0%0/1
$info->{'type'} || 'dev'
$info->{'type'}
Truth table0%0/2
A
A= $info->{'type'}
Aresult
11
00
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
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
open my $NPM, "-|", @cmd or die "Can't fork @{cmd}: $!" 0 0 0
83 0 ▶ return $NPM if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
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
BranchTrueFalseTotal
$ENV{'MOJO_NPM_CLEAN'} ? : 0 0 0
91 0 ▶ chdir(my $work_dir = path($dir ? $dir : ('local', path($0)->basename))->to_abs->tap($remove_tree)->make_path)
Branch: 2 branches
BranchTrueFalseTotal
chdir(my $work_dir = path($dir ? $dir : ("local", path($0)->basename))->to_abs->tap($remove_tree)->make_path) or die "Couldn't set up working directory: $!" 0 0 0
$dir ? : 0 0 0
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
BranchTrueFalseTotal
unless -e "node_modules" 0 0 0
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: $!"))
l!l&&r!l&&!r
000
symlink $work_dir->dirname->child("node_modules")->make_path, "node_modules" or warn "Couldn't set up shared node_modules: $!"
l!l
00
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"
ABresult
1X1
011
000
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
« Mojo/Alien/webpack.pm ↑ Summary Mojo/Alien/rollup.pm »