1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
#!/usr/bin/perl
use strict;
use Dpkg::IPC;
use Debian::PkgJs::Banned;
use Debian::PkgJs::Cache;
use Debian::PkgJs::Dependencies;
use Debian::PkgJs::Semver;
use Debian::PkgJs::Utils;
use Debian::PkgJs::Version;
use Getopt::Long;
use JSON;
use Progress::Any '$progress';
use Progress::Any::Output;
Progress::Any::Output->set( 'TermProgressBarColor',
template =>
'<color ffff00>%p%</color> <color 808000>[</color>%B<color 808000>]</color>'
);
my %opt;
GetOptions(
\%opt, qw(
h|help
v|version
audit
copy
install
install-command=s
ignore
nolink|no-link
nopackagelock|no-package-lock
regenerate
prod
all
strict
nodownload|no-download
)
);
if ( $opt{h} ) {
print <<EOF;
Install all dependencies of a JS project using Debian dependencies when
available.
Options:
-h, --help: print this
--install: launch install-command if some Debian packages are missing
--ignore: ignore missing Debian packages
--install-command: command to install mising packages. Default:
"--install-command 'sudo apt install'"
--no-link: don't link JS modules from Debian directories
--copy: copy modules instead of link them
--regenerate: force package-lock.json regeneration
--prod: don't install dev dependencies
--all: don't remove sub-dependencies of Debian packages (link them and install wanted dependencies)
--strict: download JS module if Debian version mismatch (using semver)
--no-package-lock: calculate dependencies without npm, implies --no-download. This permits one to use pkgjs-install without network connection
--audit: don't install or download anything, just print result
--no-download: only links available JS modules, don't download anything
EOF
exit;
}
elsif ( $opt{v} ) {
print "$VERSION\n";
exit;
}
$opt{nodownload} = 1 if $opt{nopackagelock};
$opt{ignore} = 1 if $opt{nodownload};
$opt{strict} = undef if $opt{nopackagelock};
if ( $opt{ignore} and $opt{install} ) {
print STDERR
"Both --install and --ignore (or --no-download) chosen, aborting\n";
exit 1;
}
$opt{'install-command'} ||= 'sudo apt install';
# Step 0: generate package-lock.json if needed
my $content;
if ( $opt{nopackagelock} ) {
scanAndInstall( '.', 'dependencies', 'peerDependencies',
( !$opt{prod} ? 'devDependencies' : () ) );
exit;
}
if ( $opt{regenerate} or not -e 'package-lock.json' ) {
eval {
spawn(
exec => [
qw(npm i --package-lock-only --legacy-peer-deps --ignore-scripts)
],
wait_child => 1,
no_check => 1,
);
};
if ($@) {
print STDERR "$@\n" . ( !$opt{regenerate} ? "Try --regenerate\n" : '' );
exit 1;
}
}
# Step 1: read package-lock.json and dispatch modules into lists:
# - Debian packages to install
# - Debian JS modules to link
# - JS modules to download
{
open my $f, 'package-lock.json' or die $!;
local $/ = undef;
$content = JSON::from_json(<$f>);
close $f;
}
unless ( $content->{packages} ) {
print STDERR
"Unable to fine 'packages' key in package-lock.json, use --regenerate option\n";
exit 1;
}
my ( %toLink, %toInstall, %toDownload, %maybeToDownload );
my $ownPackageLock = { packages => {} };
# Reduce package-lock.json to the strict needed
unless ( $opt{all} ) {
# Reduce package-lock.json to the needed dependencies only
scan(
{
dependencies => {
(
$content->{packages}->{""}->{dependencies}
? ( %{ $content->{packages}->{""}->{dependencies} } )
: ()
),
(
$content->{packages}->{""}->{peerDependencies}
? ( %{ $content->{packages}->{""}->{peerDependencies} } )
: ()
),
(
!$opt{prod} && $content->{packages}->{""}->{devDependencies}
? ( %{ $content->{packages}->{""}->{devDependencies} } )
: ()
)
}
},
''
);
$content = $ownPackageLock;
}
# Read package-lock.json content and populate:
# - Debian package to install
# - Debian modules to link
# - JS module to download and install
M: foreach my $package (
sort {
my @_a = ( $a =~ m#(node_modules/)#g );
my @_b = ( $b =~ m#(node_modules/)#g );
@_a <=> @_b || $a cmp $b;
} keys %{ $content->{packages} }
)
{
next unless $package; # Skip "" key
next unless $content->{packages}->{$package}; # Skip deleted
my $module = $package;
while ( $module =~ s#.*?node_modules/## ) {
if ( $module =~ m#(.*?)/node_modules/# ) {
next M if $toLink{$1};
}
}
$module =~ s#.*node_modules/##;
my $wantedVersion = $content->{packages}->{$package}->{version};
my $skipDownload =
( -e $package
and pjson($package)
and pjson($package)->{version}
and pjson($package)->{version} eq $wantedVersion );
if ( my $debianPackage = availableModules->{$module} ) {
$toLink{$module}++;
unless ( installedModules->{$module} ) {
push @{ $toInstall{$debianPackage} }, $module;
push @{ $maybeToDownload{$module} },
[
{
dest => $package,
url => $content->{packages}->{$package}->{resolved},
wantedVersion => $wantedVersion,
}
];
}
elsif ( $opt{strict} ) {
my $debianVersion =
pjson( installedModules->{$module} )->{version};
if (
$wantedVersion
and not( $debianVersion
and semver( $debianVersion, '^' . $wantedVersion ) )
)
{
$toLink{$module}--;
delete $toLink{$module} unless $toLink{$module};
$toDownload{$package} =
$content->{packages}->{$package}->{resolved}
unless $skipDownload;
}
}
delete $content->{packages}->{$package};
delete $content->{dependencies}->{$module};
}
else {
$toDownload{$package} = $content->{packages}->{$package}->{resolved}
unless $skipDownload;
}
}
# Summary
print scalar(%toLink)
. ' modules '
. ( $opt{nolink} ? 'already availables' : 'to link' ) . "\n";
print scalar(%toDownload) . " modules to download\n";
if ( $opt{audit} ) {
print scalar(%toInstall) . " packages to install\n" if %toInstall;
print "Missing dependencies:\n";
print join( ' ', map { s#.*/##; $_ } keys %toDownload ) . "\n";
exit;
}
# Step 2: download missing Debian packages if needed
if (%toInstall) {
unless ( $opt{install} or $opt{ignore} ) {
print STDERR "\nThe following packages are needed, choose one of "
. "--ignore or --install\n"
. join( ' ', sort keys %toInstall ) . "\n";
exit 1;
}
if ( $opt{install} ) {
print scalar(%toInstall) . " packages to install\n";
spawn(
exec => [
'sh',
'-c',
$opt{'install-command'} . ' '
. join( ' ', sort keys %toInstall )
],
wait_child => 1,
);
}
}
mkdir 'node_modules';
# Step 3: link Debian JS modules into node_modules if needed
# Reset cache
installedModules(1);
foreach my $module ( keys %toLink ) {
if ( installedModules->{$module} ) {
if ( $opt{strict} and $maybeToDownload{$module} ) {
my $continue = 0;
foreach my $tmp ( @{ $maybeToDownload{$module} } ) {
my $debianVersion =
pjson( installedModules->{$module} )->{version};
if (
$tmp->{wantedVersion}
and not( $debianVersion
and
semver( $debianVersion, '^' . $tmp->{wantedVersion} ) )
)
{
$toDownload{ $tmp->{path} } = $tmp->{url};
}
else {
$continue = 1;
}
}
next unless $continue;
}
unless ( $opt{nolink} ) {
if ( $module =~ m#(.*)/# ) {
mkdir "node_modules/$1";
}
spawn(
exec => [ 'rm', '-rf', "node_modules/$module" ],
wait_child => 1
);
if ( $opt{copy} ) {
spawn(
exec => [
'cp', '-a',
installedModules->{$module}, "node_modules/$module"
],
wait_child => 1
);
}
else {
symlink installedModules->{$module}, "node_modules/$module";
}
}
}
}
# Step 4: download missing
my %from;
unless ( $opt{nodownload} ) {
$progress->target( scalar %toDownload );
my $notCache = not( -e "$ENV{HOME}/.npm/_cacache" );
if (%toDownload) {
print "Downloading...\n";
foreach my $modulePath ( keys %toDownload ) {
$progress->update( message => $modulePath );
$from{
downloadAndInstall( $modulePath, $toDownload{$modulePath},
undef, $notCache )
}++;
}
$progress->finish();
print "Done (" . join(
',',
map {
$from{$_}
? $from{$_} . ' '
. {
0 => 'not found',
1 => 'downloaded',
2 => 'from npm cache'
}->{$_}
: ()
} ( 0 .. 2 )
) . ")\n";
}
}
# Internal subroutine to reduce package-lock.json
my %seen;
sub scan {
my ( $package, $offset ) = @_;
return if $seen{$package};
$seen{$package}++;
return
unless $package->{dependencies} and %{ $package->{dependencies} };
foreach my $dep ( keys %{ $package->{dependencies} } ) {
next if $ownPackageLock->{packages}->{"$offset/node_modules/$dep"};
my $tmp = $offset;
my $continue = 1;
D: do {
if ( $continue
and $content->{packages}->{"${tmp}node_modules/$dep"} )
{
$ownPackageLock->{packages}->{"${tmp}node_modules/$dep"} =
$content->{packages}->{"${tmp}node_modules/$dep"};
scan( $content->{packages}->{"${tmp}node_modules/$dep"},
"${tmp}node_modules/$dep/" )
unless availableModules->{$dep};
$continue = 0;
}
} while ( $tmp =~ s#node_modules/.*?$## );
}
}
sub scanAndInstall {
my ( $path, @fields ) = @_;
@fields = ( 'dependencies', 'peerDependencies' ) unless @fields;
my @deps;
foreach (@fields) {
my $tmp;
if ( ( $tmp = pjson($path)->{$_} ) and ref $tmp ) {
push @deps, keys %$tmp;
}
}
mkdir 'node_modules';
foreach my $mod (@deps) {
next if -e "node_modules/$mod";
next unless installedModules->{$mod};
if ( $mod =~ m#(.*)/# ) {
mkdir "node_modules/$1";
}
if ( $opt{copy} ) {
spawn(
exec =>
[ 'cp', '-a', installedModules->{$mod}, "node_modules/$mod" ],
wait_child => 1,
);
scanAndInstall("node_modules/$mod");
}
else {
symlink installedModules->{$mod}, "node_modules/$mod";
}
}
}
|