File: pkgjs-utils

package info (click to toggle)
pkg-js-tools 0.16.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,992 kB
  • sloc: perl: 4,975; sh: 840; makefile: 36; javascript: 22
file content (349 lines) | stat: -rwxr-xr-x 8,995 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;
use Debian::PkgJs::Utils;
use Debian::PkgJs::Version;
use JSON;
use Pod::Usage;
use Pod::Text;

my $cmd = $0 =~ m#/pkgjs-(ln|pjson|main)$# ? $1 : shift(@ARGV);

pod2usage(
    -message => 'Missing command',
    -exitval => 1,
) unless $cmd;

pod2usage(
    -message => 'Bad command',
    -exitval => 2,
  )

  # $ regexp-assemble <<EOF
  # link_test_modules
  # link_external_modules
  # link_internal_modules
  # link_build_modules
  # clean_test_modules
  # clean_external_modules
  # clean_internal_modules
  # clean_build_modules
  # ln
  # components_list
  # modules_list
  # packages_list
  # root_components_list
  # root_modules_list
  # ordered_components_list
  # pjson
  # main
  # normalize_name
  # -h
  # -v
  # --help
  # --version
  # EOF
  unless $cmd =~
/^(?:c(?:lean_(?:(?:ex|in)ternal|build|test)_modules|omponents_list)|(?:root_(?:component|module)|ordered_component)s_list|l(?:ink_(?:(?:ex|in)ternal|build|test)_modules|n)|-(?:[hv]|-(?:version|help))|p(?:ackages_list|json)|m(?:odules_list|ain)|normalize_name)$/;

our $obj = bless {}, 'Debian::PkgJs::Utils';

use constant _sub => {
    '-v' => sub {
        return $VERSION;
    },
    '-h' => sub {
        return pod2usage( -verbose => 2 );
    },
    main => sub {
        return $obj->pjson( $obj->main_package );
    },
    components_list => sub {
        my $res = $obj->component_list;
        return join( "\n", sort keys %$res );
    },
    modules_list => sub {
        my $res = $obj->component_list;
        return join( "\n", sort values %$res );
    },
    normalize_name => sub {
        return $obj->normalize_name(@ARGV);
    },
    root_components_list => sub {
        my $res = $obj->root_components_list;
        return join( "\n", sort keys %$res );
    },
    packages_list => sub {
        return join( "\n", $obj->packages_list );
    },
    root_modules_list => sub {
        my $res = $obj->root_components_list;
        return join( "\n", sort values %$res );
    },
    ordered_components_list => sub {
        my @res = $obj->ordered_components_list;
        return join( "\n", @res );
    },
    pjson => sub {
        my $cmp = shift(@ARGV) || '.';
        my $res = $obj->pjson($cmp);
        while ( $_ = shift @ARGV ) {
            if ( ref $res ) {
                if ( defined $res->{$_} ) {
                    $res = $res->{$_};
                }
                else {
                    die "Unable to find $_ in $cmp/package.json" if (@ARGV);
                    $res = '';
                }
            }
            else {
                pod2usage(
                    -message => 'Bad json path',
                    -exitval => 3,
                );
            }
        }
        my $ref = ref($res) || '';
        return
            $ref eq 'HASH'  ? JSON::to_json($res)
          : $ref eq 'ARRAY' ? join( "\n", @$res )
          :                   $res;
    },
    main => sub {
        my $dir = '.';
        if ( -e 'debian/nodejs/main' ) {
            local $/ = undef;
            open my $f, 'debian/nodejs/main' or die $!;
            $dir = <$f>;
            close $f;
            $dir =~ s/\s//g;
        }
        return $obj->pjson($dir)->{name};
    },
    ln => sub {
        my $mod = shift @ARGV;
        die 'Missing arg' unless $mod;
        my $displayName = $mod;
        if ( $mod eq '-t' ) {
            $mod                          = shift @ARGV;
            $Debian::PkgJs::Utils::WANTTS = 1;
            $displayName                  = "(TS) $mod";
        }
        my $res = $obj->ln($mod);
        if ($res) {
            return $res < 0
              ? "$displayName exists"
              : "$displayName linked into node_modules";
        }
        print STDERR "Unable to link $mod: $!\n";
        exit 1;
    },
    link_build_modules => sub {
        $obj->link_build_modules;
        return '';
    },
    link_external_modules => sub {
        $obj->link_external_modules;
        return '';
    },
    link_internal_modules => sub {
        $obj->link_internal_modules;
        return '';
    },
    link_test_modules => sub {
        $obj->link_test_modules;
        return '';
    },
    clean_build_modules => sub {
        $obj->clean_build_modules;
        return '';
    },
    clean_external_modules => sub {
        $obj->clean_external_modules;
        return '';
    },
    clean_internal_modules => sub {
        $obj->clean_internal_modules;
        return '';
    },
    clean_test_modules => sub {
        $obj->clean_test_modules;
        return '';
    },
};

_sub->{'--version'} = _sub->{'-v'};
_sub->{'--help'}    = _sub->{'-h'};

if ( $cmd =~ /_list$/ ) {
    $OPTS->{lerna} =
      !search_bd_in_debian_control(qr/^dh-sequence-nodejs-no-lerna$/);
}
my $res = _sub->{$cmd}->();
chomp $res;
print $res. ( $res =~ /./ ? "\n" : '' );
__END__
=head1 NAME

pkgjs-utils - Nodejs package utilities

=head1 SYNOPSIS

  $ cd node-glob
  $ pkgjs-utils main
    # prints: glob
  $ pkgjs-main
    # alias for pkgjs-utils main
  $ pkgjs-utils components_list
    # prints:
    #  globalyzer
    #  globrex
    #  tiny-glob
    #  types-glob
  $ pkgjs-utils modules_list
    # prints:
    #  globalyzer
    #  globrex
    #  tiny-glob
    #  @types/glob
  $ pkgjs-utils pjson .
    # prints: { <package.json content, serialized> }
  $ pkgjs-pjson .
    # alias for pkgjs-utils pjson .
  $ cd ../node-jest
  $ pkgjs-pjson packages/jest-diff
    # prints: { <packages/jest-diff/package.json content, serialized> }
  $ pkgjs-pjson packages/jest-diff version
    # prints: 27.2.5
  $ pkgjs-utils pjson . scripts test
    # prints: jest
  $ pkgjs-utils pjson packages/jest-diff scripts test
  $ pkgjs-utils ln webpack
    # prints: 'webpack linked into node_modules
  $ pkgjs-utils ln @types/node
    # prints: '@types/node' linked into node_modules
  $ pkgjs-ln @types/node
    # same as pkgjs-utils ln @types/node
  $ pkgjs-utils normalize_name @types/node
    # prints: types-node
  $ pkgjs-utils root_components_list (only components installed in nodejs dir)
  $ pkgjs-utils root_modules_list (same with module names)
  $ pkgjs-utils ordered_components_list (components in build order)

=head1 DESCRIPTION

Collection of little commands to read <component>/package.json overridden by
debian/nodejs/<component>/* files.

=head2 Available commands

=over

=item B<link_test_modules>

Link modules that are in the B<debian/tests/test_modules> directory into
"node_modules".

=item B<link_external_modules>

Link Debian modules listed into B<debian/nodejs/extlinks> into "node_modules"
and copy modules listed into B<debian/nodejs/extcopies> into "node_modules".

=item B<link_internal_modules>

In multi-modules package I<(with B<uscan> components and/or lerna/workspace)>,
link these modules into "node_modules", except the "main" module.

=item B<link_build_modules>

Link modules that are in the B<debian/build_modules> directory into
"node_modules".

=item B<clean_test_modules>

Drop links created by B<link_test_modules>.

=item B<clean_external_modules>

Drop links/copies created by B<link_external_modules>.

=item B<clean_internal_modules>

Drop links created by B<link_internal_modules>.

=item B<clean_build_modules>

Drop links created by B<link_build_modules>.

=item B<ln>

Link the given Debian module into "node_modules".

If "-t" option is set, try to link the corresponding typescript declarations.
For example:

    $ pkgjs-ln -t glob; pkgjs-ln -t abab

creates "node_modules/@types/glob" and "node_modules/abab".

=item B<components_list>

Print the list of components (directories) without the "main" module.

=item B<modules_list>

Print the NPM names of components without the "main" module.

=item B<pjson>

Reads the package.json I<or package.yaml> of the given directory. If no
arguments are given, it prints the serialized JSON content. If arguments
are given, it tries to follow keys. For example:

    $ pkgjs-pjson packages/foo scripts run

write the content of the subkey "run" of the key "scripts".

=item B<main>

Print the NPM name of the "main" module.

=item B<packages_list>

Print the list of binary packages from "debian/control".

=item B<normalize_name>

Convert a NPM module name into a Debian compatible name. Examples:

    $ pkgjs-utils normalize_name @types/glob
    type-glob
    $ pkgjs-utils normalize_name @types/babel__type
    types-babel-type
    $ pkgjs-utils normalize_name require_optional
    require-optional

=item B<-h> B<--help>

=item B<-v> B<--version>

=back

=head1 AUTHOR

Yadd <yadd@debian.org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) Yadd <yadd@debian.org>

This program 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.
.
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'

=cut