File: PackageLock.pm

package info (click to toggle)
pkg-js-tools 0.17.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,036 kB
  • sloc: perl: 5,006; sh: 840; makefile: 36; javascript: 22
file content (222 lines) | stat: -rw-r--r-- 6,608 bytes parent folder | download
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
package Debian::PkgJs::PackageLock;

use strict;
use Debian::Control;
use Debian::PkgJs::Utils;
use Debian::PkgJs::Version;
use Dpkg::IPC;
use Exporter 'import';
use JSON;

our @EXPORT = ( '&buildPackageLock', '&builtUsing' );

our $BUILTUSING = {};
our $PACKAGES   = {};
our $PATHS      = {};

my $WEBPACKS =
qr/^.*\b(?:node-(?:rollup-plugin-node-resolve|esbuild)|browserify(?:-lite)?|esbuild|webpack)\b.*/i;
*debug = \&Debian::PkgJs::Utils::debug;

sub nodepathNoError {
    my ( $related, $jobs, @mods ) = @_;
    $jobs ||= 1;
    my (%res);
    while (@mods) {
        my @PIDS;
        my @list = splice @mods, 0, $jobs;
        foreach my $mod (@list) {
            if ( $PATHS->{$mod} and ( $related or $PACKAGES->{$mod} ) ) {
                $res{$mod} = [ $PACKAGES->{$mod}, $PATHS->{$mod} ];
            }
            else {
                my ( $read, $null );
                $read = IO::Handle->new;
                open $null, '>', '/dev/null';
                push @PIDS,
                  [
                    $mod,
                    spawn(
                        exec =>
                          [ 'nodepath', ( $related ? '-pr' : '-p' ), $mod ],
                        to_pipe         => $read,
                        error_to_handle => $null,
                        wait_child      => 0,
                    ),
                    $read, $null,
                  ];
            }
        }
        foreach (@PIDS) {
            my ( $mod, $pid, $out, $null ) = @$_;
            eval {
                my $p = wait_child( $pid, cmdline => 'z' );
                my ( $pkg, $path ) = split /: /, <$out>;
                chomp $path;

                # Fix reproducibility
                unless (
                    $path =~ s#.*(/usr/(?:share|lib(?:/[^/]+)?)/nodejs/)#$1# )
                {
                    $path =~ s#^/.*$#node_modules/$mod#;
                }
                $PACKAGES->{$mod} = $pkg if $pkg;
                if ($path) {
                    $PATHS->{$mod} = $path;
                    $res{$mod} = [ $pkg, $path ];
                }
            };
            close $out;
            close $null;
        }
    }
    return \%res;
}

sub buildPackageLock {
    my ( $src, $dst, $check, $jobs ) = @_;
    die 'Destination needed' unless $dst;
    if ($check) {
        return unless search_bd_in_debian_control($WEBPACKS);
        debug("Package looks like a bundle, generating pkgjs-lock.json file");
    }
    my $pjson = pjson($src);
    my $deps  = {
        %{ $pjson->{devDependencies}  // {} },
        %{ $pjson->{dependencies}     // {} },
        %{ $pjson->{peerDependencies} // {} },
    };
    my $res = {
        name            => $pjson->{name},
        version         => $pjson->{version},
        lockfileVersion => 2,
        packages        => {
            "" => {
                name    => $pjson->{name},
                version => $pjson->{version},
            },
        },
        dependencies  => {},
        pkgjs_version => $VERSION,
    };
    my $count = 0;
    if ( $deps and %$deps ) {
        my $nodePaths = nodepathNoError(
            1,
            ( $jobs // 1 ),
            map { -d "node_modules/$_" ? () : $_ } keys %$deps
        );
        foreach my $dep ( keys %$deps ) {
            my ( $out, $err );
            if ( -d "node_modules/$dep" ) {
                $out = "node_modules/$dep";
            }
            else {
                $out = $nodePaths->{$dep}->[1];
            }
            if ( $out and my $pjs = pjson($out) ) {
                $count++;
                $res->{packages}->{$out} = {
                    name    => $dep,
                    version => $pjs->{version} || 'unknown',
                };
                $res->{dependencies}->{$dep} =
                  { version => $pjs->{version} || '*', };
                $BUILTUSING->{$dep} = $pjs->{version} || '';
            }
        }
    }
    unless ($count) {
        debug("No build dependencies found, skip pkgjs-lock.json");
        return;
    }
    open my $fdst, '>', $dst or die "Unable to write package-lock.json: $!";
    my $str = JSON->new->canonical->indent->encode($res);
    $str =~ s/   / /g;
    chomp $str;
    print $fdst $str;
    close $fdst;
    return 1;
}

sub builtUsing {
    my ( %res, %seen );
    my $nodePaths = nodepathNoError( 0, ( $_[0] // 1 ), keys %$BUILTUSING );
    map {
        my ( $pkg, $version_source, $err );
        if ( $pkg = $nodePaths->{$_}->[0] ) {
            eval {
                unless ( $seen{$pkg} ) {
                    spawn(
                        exec => [
                            'dpkg-query', '--showformat=${Version},${Source}',
                            '--show',     $pkg,
                        ],
                        wait_child      => 1,
                        to_string       => \$version_source,
                        error_to_string => \$err,
                    );
                    chomp $version_source;
                    my @vs = split /,/, $version_source;

                    # Source is empty if the binary package has the same name
                    $vs[1] ||= $pkg;
                    $res{ $vs[1] } = $vs[0];
                    $seen{$pkg} = 1;
                    debug("Add $vs[1] (= $vs[0]) in \${nodejs:BuiltUsing}");
                }
            };
        }
    } keys %$BUILTUSING;
    return join( ',', map { "$_ (= $res{$_})" } sort keys %res );
}

1;
__END__
=head1 NAME

Debian::PkgJS::PackageLock - Perl module to build pkgjs-lock.json files

=head1 SYNOPSIS

  use Debian::PkgJS::PackageLock;
  
  for my $path (@jsModulePaths) {
      buildPackageLock( $path, "$path/pkgjs-locj.json");
  }
  
  use Debian::Debhelper::Dh_Lib;
  
  addsubstvar( $pkg, builtUsing() );

=head1 DESCRIPTION

=head2 EXPORT

=over

=item B<buildPackageLock( $path, $dest )>

=item B<builtUsing()>

=back

=head1 COPYRIGHT AND LICENSE

Copyright Yadd E<lt>yadd@debian.orgE<gt>

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

On Debian systems, the complete text of version 2 of the GNU General
Public License can be found in `/usr/share/common-licenses/GPL-2'.
If not, see L<http://www.gnu.org/licenses/>.

=cut