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
|
#!/usr/bin/perl
use strict;
use Dpkg::IPC;
use Debian::PkgJs::Dependencies;
use Debian::PkgJs::Utils;
use Debian::PkgJs::Version;
use Getopt::Long;
use JSON;
my %opt;
GetOptions(
\%opt, qw(
h|help
v|version
copy
prod|production
)
);
if ( $opt{h} ) {
print <<EOF;
Link or copy all available dependencies of a JS project using Debian dependencies.
Options:
-h, --help: print this
--copy: copy modules instead of link them
--prod: don't install dev dependencies
EOF
exit;
}
elsif ( $opt{v} ) {
print "$VERSION\n";
exit;
}
# Step 0: generate package-lock.json if needed
scanAndInstall( '.', 'dependencies', 'peerDependencies',
( !$opt{prod} ? 'devDependencies' : () ) );
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";
}
}
}
|